Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine PDFs with Active Storage

I am trying to combine multiple pdfs into a single downloadable file. I have a model with multiple room names and email room has a pdf attached that is the layout of the room. The goal is to be able to download all room layouts based on a list of rooms. I found the gem combine_pdf and it looks great, simple, but I feel like I am missing something. I get a lot of "No such file or directory @ rb_sysopen -" but when I cut and paste the following link, it works fine.

Current Code

 def index
  @forms = Form.all
  @forms.each do |form|
    pdf = CombinePDF.new
    pdf << CombinePDF.load(url_for(form.image))
 end
end

I haven't got to the download part, but the docs say to use send_data combined_file.to_pdf, filename: "combined.pdf", type: "application/pdf"

but how would I have the view access that from the controller and make it downloadable?

Update: I was able to get passed all errors with the following code. But I can't figure out how to create a link to download

def index
 @forms = Form.all
 @pdf = CombinePDF.new
 @forms.each do |form|
   file = open('http://evanliewer.com/orientation/test.pdf')
   @pdf << CombinePDF.load(file.to_path)
 end
 @pdf.save "combined.pdf"
end
like image 228
user1895399 Avatar asked Sep 02 '25 15:09

user1895399


1 Answers

Was able to figure it out. Below is the controller:

require 'net/http'

def viewproject
  @forms = Form.all
  respond_to do |format|
    format.html
    format.pdf do
    pdf = CombinePDF.new
    @forms.each do |form|
      pdf << CombinePDF.parse( Net::HTTP.get( URI.parse( url_for(form.image.service_url) ) ) )
    end
    send_data pdf.to_pdf, :disposition => 'inline', :type => "application/pdf"
  end
end

end

And the View is

<%= link_to "Printable Receipt (PDF)", cabinpdf_path(@forms, format: 'pdf', disposition: "attachment") %>

Im going back now to clean it all up, but was glad I was able to get it. The two secrets I missed earlier was the active record call to find the url works best if you use .service_url and I used the require 'net/http' to access it that way as the path wasn't working since it wasn't stored locally. It currently isn't working on my local (using local storage there), but is working in production with heroku.

like image 197
user1895399 Avatar answered Sep 05 '25 15:09

user1895399