Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

pdf generation - How to use CombinePDF with Rails 6 (ActiveStorage) on heroku

I generate PDF documents using Prawn in Rails. For years I have only inserted jpg's into PDFs. More recently I've had a need to insert an external PDF into a Prawn PDF. The way to do this seems to be to merge PDFs together using CombinePDF.

I have this functionality working successfully IF I use a local document. As soon as ActiveStorage gets in the mix, it stops working...it times out.

  • Rails -v 6.1.1
  • Ruby -v 2.7.2p137
  • Prawn -v 2.4
  • CombinePDF -v 1.0.21
  • on Heroku using Amazon S3

The test file I am using is only 50k in size. The exact same thing occurs whether I attempt this in Development or Production (on heroku). In the logs I can see:

S3 Storage (716.2ms) Downloaded file from key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

And, then ....

Request ran for longer than 28000ms

Gemfile

gem "combine_pdf"

PdfsController

    class PdfsController < ApplicationController
      require 'combine_pdf'
      require 'net/http'
      include Rails.application.routes.url_helpers

      def the_report
        thing = Thing.find params[:thing_id]
        documents = Document.with_attached_attachments.where(id: thing.document_ids).joins(attachments_attachments: :blob).where(blob: {content_type: 'application/pdf'})
        respond_to do |format|
          format.pdf do
            prawn_pdf = TheReport.new(params[:thing_id]).render
            final_pdf = CombinePDF.new

            final_pdf << CombinePDF.parse(prawn_pdf)
            
            documents do |doc|
              doc.attachments.each do |attachment|
                # time out occurs here
                url = rails_blob_url(attachment, only_path: true)
                final_pdf << CombinePDF.parse(Net::HTTP.get_response(URI.parse(url)).body)
              end
            end

            send_data final_pdf.to_pdf, filename: "thing.pdf", type: 'application/pdf', disposition: 'inline', compress: true, optimize_objects: true
          end
        end
      end
    end

The request at Net::HTTP.get_response seems to be taking too long. How can I trouble shoot this, and/or is there a better way to accomplish this?

question from:https://stackoverflow.com/questions/65855647/how-to-use-combinepdf-with-rails-6-activestorage-on-heroku

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

After a bunch of trial and error it seems it was as simple as

changing this...

documents do |doc|
 doc.attachments.each do |attachment|
  url = rails_blob_url(attachment, only_path: true)
  final_pdf << CombinePDF.parse(Net::HTTP.get_response(URI.parse(url)).body)
 end
end

to this...

documents do |doc|
 doc.attachments.each do |attachment|
  final_pdf << CombinePDF.parse(Net::HTTP.get_response(URI.parse(attachment.url)).body)
 end
end

url method docs


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...