【问题标题】:Prawn - image is cropped & full image not render大虾 - 图像被裁剪且完整图像未渲染
【发布时间】:2018-11-07 16:02:15
【问题描述】:

我目前正在使用 Prawn 和 CombinePDF 开发一个 Ruby on Rails 5.2 应用程序。我请求将文件发送到 Box api。我从框中获得的文件然后使用StringIO.new(image) 转换为字符串,然后将其添加到虾文档中,如图所示。

attach_docs_from_submission(user_data).each do |file|
      image = client.download_file(file)
      format_image = StringIO.new(image)
      pdf_prawn = Prawn::Document.new
      pdf_prawn.image format_image
      pdf_data = pdf_prawn.render
      pdf << CombinePDF.parse(pdf_data)
    end

但是,当我检查 PDF 不是完整图像时。

有没有办法适应全尺寸的图像?问题是来自 Prawn 还是 CombinePDF?

【问题讨论】:

    标签: ruby ruby-on-rails-5 prawn


    【解决方案1】:

    这个答案是基于我的偏好,因为我意识到有很多方法可以解决这个问题。对我个人而言,这是最精简、最灵活的解决方案。

          pdf_prawn = Prawn::Document.new(page_size: 'LETTER')
          pdf_prawn.image format_image, fit: [610, 790],
                                        position: :center,
                                        position: :center
    

    因此,您将page_size 设置为LETTER,然后获取字母的尺寸并将其作为参数传递给fit:,同时考虑页面上的.5 边距。您也可以使用scale: 调整图像大小,但我发现它缺乏精确度。有关这方面的更多信息,请参阅Prawn 手册

    【讨论】:

      【解决方案2】:

      您可以使用以下方法获取页面尺寸:

      pdf_prawn.bounds.width
      pdf_prawn.bounds.height
      

      你可以通过这些来适应。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-01
        • 2021-05-05
        • 2011-11-11
        • 1970-01-01
        • 2012-04-10
        • 2013-03-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多