ruby-on-rails - Ruby on Rails 事件存储中的 Joint_PDF 解析错误

标签 ruby-on-rails rails-activestorage combine-pdf

我一直在修改Combine_PDF来为上传的文档添加水印。我已经设法让它结合本地托管的文件或通过使用 net/http 请求来工作。我希望它在用户下载存储在事件存储中的文件时为其添加水印。我在终端中不断收到此错误:

 Warning: parser advancing for unknown reason. Potential data-loss.

网页上是这样的:

Unknown PDF parsing error - malformed PDF file?

检查文档,这是这个 gem 的一个非常普遍的错误。上传到 Active Storage 的 PDF 文件与我在 net/http 解析功能中使用的网站下载的 PDF 文件相同。这是我的代码:

require 'combine_pdf'
require 'net/http'
def show
    user = User.last
    respond_to do |format|
      format.html
      format.pdf do  
        pdf = CombinePDF.new
        url = url_for(@script.document)
        pdf = CombinePDF.parse Net::HTTP.get_response(URI.parse(url)).body
        pdf.pages.each {|page| page.textbox "#{user.first_name} #{user.last_name}", height: 70, width: 400, y: 200, x: 25}
        send_data pdf.to_pdf, filename: "combined.pdf", 
                                          type: "application/pdf",
                                          disposition: "inline"
      end
    end
  end

正如我之前所说,从外部托管的 pdf 中提取数据时,此设置工作正常:

require 'combine_pdf'
  require 'net/http'
   def show
    user = User.last
    respond_to do |format|
      format.html
      format.pdf do  
        pdf = CombinePDF.new
        url = "https://www.americanexpress.com/content/dam/amex/us/staticassets/pdf/GCO/Test_PDF.pdf"  
        pdf = CombinePDF.parse Net::HTTP.get_response(URI.parse(url)).body
        pdf.pages.each {|page| page.textbox "#{user.first_name} #{user.last_name}", height: 70, width: 400, y: 200, x: 25}
        send_data pdf.to_pdf, filename: "combined.pdf", 
                                          type: "application/pdf",
                                          disposition: "inline"
      end
    end
  end

如您所见,我使用了美国运通的测试 PDF,效果很好。我可以假设这只是 Active Storage 的问题。如果可能的话,我宁愿不要弄乱临时文件。

非常感谢任何帮助!提前感谢大家。

最佳答案

尝试替换:

pdf = CombinePDF.new
url = url_for(@script.document)
pdf = CombinePDF.parse Net::HTTP.get_response(URI.parse(url)).body

...与:

pdf = @script.document.open { |f| CombinePDF.load(f.path) }

关于ruby-on-rails - Ruby on Rails 事件存储中的 Joint_PDF 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68322491/

相关文章:

ruby-on-rails - Rails API 空请求 header

ruby-on-rails - 将事件的存储附件下载到光盘

ruby-on-rails - 如何使用 Active Storage Rails 上传到采用 AES256 加密(服务器端加密)的 AWS S3 存储桶?

rails-activestorage - Rails 和 active_storage : Cannot load `Rails.config.active_storage.service` : (RuntimeError) Missing service adapter for "S3"

sql - Rails 中的延迟作用域

ruby-on-rails - 使用 Postgresql 在 OS X Lion 上安装 Sphinx

ruby-on-rails - 如何在 Rails 中制作 UI 组件?