3

In a Rails 4 project I have been tasked with merging an “about” page to the end of PDF documents that have been uploaded via Paperclip.

The issue is that some of the uploaded pdfs contain optional content. I started out using combine_pdf but it does not support files with optional content as explained here. I have tried Prawn but it no longer support this functionality. I finally found the PDF Toolkit gem but the documentation does not say anything about optional content support. PDF Toolkit is a command line tool that has been wrapped in a Gem and therefore operates outside of the Rails application. I have tried using these command line examples in my CLI: pdftk file1.pdf file2.pdf cat output out_file.pdf but the terminal just hangs indefinitely.

The gem's documentation here is very unclear (to me) and may be the source of my issues.

My hope is to find advice on how to accomplish this using PDF Toolkit OR a better library for merging PDFs with optional content.

I have gotten this far by researching Stack Overflow questions like this and this

My OS is OSX 10.11.6

existing_pdf_path = @report.document.file.path

# Create a html template and convert it to pdf
about_company_html = render_to_string("_about_company.html.erb", layout: false)

about_company_pdf = WickedPdf.new.pdf_from_string(about_company_html, orientation: 'Landscape')

# Save about_company_pdf to file in tmp/pdf
about_company_pdf_path = Rails.root.join('tmp/pdf', 'about_company_partial.pdf').to_s
File.open(about_company_pdf_path, 'wb') { |file| file << about_company_pdf }

# Create and save a blank target file we will save everything to
combined_pdf_path = Rails.root.join('tmp/pdf', 'combined.pdf').to_s
FileUtils.touch(combined_pdf_path)

# This returns false OR just hangs depending on my exact syntax, no error or backtrace
result = PDF::Toolkit.pdftk( *%w(existing_pdf_path about_company_pdf_path cat output combined_pdf_path) )

I have tried as many variations of the above call to pdftk as I can find or think of. For example PDF::Toolkit.pdftk( existing_pdf_path, about_lux_pdf_path, 'cat', 'output', combined_pdf_path ) with no results.

Community
  • 1
  • 1
NickTerrafranca
  • 109
  • 1
  • 8
  • 1
    Your link to CombinePDF mentions a PR... Did you try `CombinePDF.load(about_company_pdf_path, :allow_optional_content)`? Also, is it possible to use WickedPdf without optional content? I didn't try this, but what about the `print_media_type: true`? – Myst Feb 02 '17 at 01:02
  • Nick, did you ever figure this out? – amorimluc Aug 28 '21 at 01:25

0 Answers0