How can I merge all PDF in one file with wickedpdf?

wicked_pdf docs
combine pdf gem
combine pdf github
wicked pdf save to file
wicked-pdf margins
wicked-pdf css
combinepdf
wicked pdf show as html

The way I'm working right now is I'm generating multiple pdf files and sending them all one at a time for the user to download, but the problem is sometimes they end up with too many files.

How can I merge all pdfs in a single file before sending them to the user?

Try PDFtk. In my opinon, it is the best library for editing PDF files, and there are some gems that wraps it for access from Ruby.

Merging PDFs � Issue #339 � mileszs/wicked_pdf � GitHub, wkhtmltopdf can take multiple pdf files (or urls), and render them all as one pdf with a command similar to this: wkhtmltopdf tmp/tempfile1.html� How to combine PDF files online: Drag and drop your PDFs into the PDF combiner. Rearrange individual pages or entire files in the desired order. Add more files, rotate or delete files, if needed. Click ‘Merge PDF!’ to combine and download your PDF.

I use combine_pdf gem.

To combine PDF files:

pdf = CombinePDF.new
pdf << CombinePDF.load("file1.pdf") # one way to combine, very fast.
pdf << CombinePDF.load("file2.pdf")
pdf.save "combined.pdf"

You can also parse PDF files from memory. Loading from the memory is especially effective for importing PDF data recieved through the internet or from a different authoring library such as Prawn:

pdf_data = prawn_pdf_document.render # Import PDF data from Prawn
pdf = CombinePDF.parse(pdf_data)

Merge multiple pdfs into one � Issue #273 � mileszs/wicked_pdf , Is there some way to create multiple temporary pdfs (with different orientations) and merge them into one pdf file and retain the page numbers� You can merge PDFs or a mix of PDF documents and other files. Arrange and delete content: Click, drag, and drop to reorder files or press "Delete" to remove any content you don't want. Combine files: When you're finished arranging, click "Combine Files". Save as a PDF file: Name your file and click the "Save" button. That's it.

If you want to use some tool like PDFTk or CombinePDF, all you should need to do is prerender your individual PDFs by using something like:

pdf1 = render_to_string(pdf: 'pdf1', template: 'pdf1')
pdf2 = render_to_string(pdf: 'pdf2', template: 'pdf2')

or

pdf1 = WickedPdf.new.pdf_from_string(some_html_string)
pdf2 = WickedPdf.new.pdf_from_string(another_html_string)

If those tools won't take a PDF as a string, you may need to write them to tempfiles first.

If you don't want to introduce another dependency to merge things, wkhtmltopdf can take multiple pdf files (or urls), and render them all as one pdf with a command similar to this:

wkhtmltopdf tmp/tempfile1.html tmp/tempfile2.html tmp/output.pdf

Knowing this, you could prerender your templates, with layouts and everything, out to HTML strings, then pass them into wkhtmltopdf something like this:

# app/models/concerns/multipage_pdf_renderer.rb
require 'open3'
class MultipagePdfRenderer
  def self.combine(documents)
    outfile = WickedPdfTempfile.new('multipage_pdf_renderer.pdf')

    tempfiles = documents.each_with_index.map do |doc, index|
      file = WickedPdfTempfile.new("multipage_pdf_doc_#{index}.html")
      file.binmode
      file.write(doc)
      file.rewind
      file
    end

    filepaths = tempfiles.map{ |tf| tf.path.to_s }

    binary = WickedPdf.new.send(:find_wkhtmltopdf_binary_path)

    command = [binary, '-q']
    filepaths.each { |fp| command << fp }
    command << outfile.path.to_s
    err = Open3.popen3(*command) do |stdin, stdout, stderr|
      stderr.read
    end

    raise "Problem generating multipage pdf: #{err}" if err.present?
    return outfile.read
  ensure
    tempfiles.each(&:close!)
  end
end

And call in your controller something like this:

def fancy_report
  respond_to do |format|
    format.pdf do
      doc1 = render_to_string(template: 'pages/_page1')
      doc2 = render_to_string(template: 'pages/_page2')
      pdf_file = MultipagePdfRenderer.combine([doc1, doc2])
      send_data pdf_file, type: 'application/pdf', disposition: 'inline'
    end
  end
end

However, this only covers the simplest of cases, you'll have to do the work of rendering the headers and footers if you need them, parsing (or adding) any options you might need.

This solution originally came from https://github.com/mileszs/wicked_pdf/issues/339 so it may be helpful to look there for more details on this strategy.

Render and combine PDF files into one – Today I Learned, Render PDF files from HTML templates in Rails can be done using WickedPDF. First you need to use ActionView to render HTML to a string: You can merge PDFs or a mix of PDF documents and other files. Arrange and delete content: Click, drag and drop to reorder files or press "Delete" to remove any content you don't want. Combine files: When you're finished arranging, click "Combine Files". Save as a PDF file: Name your file and click the "Save" button. That's it.

Category: PDF Generation, Wicked PDF uses the shell utility wkhtmltopdf to serve a PDF file to a user from PDF files, manipulating existing PDF files, merging multiple PDF files into one,� You can merge PDFs or a mix of PDF documents and other files. Arrange and delete content: Click, drag, and drop to reorder files or press "Delete" to remove any content you don't want. Combine files: When you're finished arranging, click "Combine Files". Save as a PDF file: Name your file and click the "Save" button. That's it.

File: README — Documentation for wicked_pdf (0.9.10), Wicked PDF uses the shell utility wkhtmltopdf to serve a PDF file to a user One simple way to install all of the binaries (Linux, OSX, Windows) is through the everyone else for all their hard work and patience with my delays in merging in� It lets you merge or combine multiple PDF files into one based on relevance, and later you can use the resulting PDF file to access all the data. You can merge PDF files offline on multiple bases (i.e., by specific Page numbers, by Even & odd Pages, or by page range) to make them easily accessible.

File: README — Documentation for wicked_pdf (1.0.4), Wicked PDF uses the shell utility wkhtmltopdf to serve a PDF file to a user from everyone else for all their hard work and patience with my delays in merging in� Similarly, open all files and After the little arrangement of Data, you can simply save it as a New combined PDF file. BOOM! You are Done. From the Editor’s Desk. So, that is all from My Side about the Combine and Merge all PDF into One PDF File. If you have any doubt or you want to suggest anything to add in the article.

Comments
  • My PDF's are generated dinamically, will that be a problem when using PDFtk? Since the contents in my PDF change from each user.
  • You can apply PDFtk each time.
  • Take a look at its syntax and see if you can handle that, it you are interested. It is capable of combining multiple files.