Reputation: 4830
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.
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
gem "combine_pdf"
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?
Upvotes: 1
Views: 1077
Reputation: 4830
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
Upvotes: 1