boazsegev / combine_pdf

A Pure ruby library to merge PDF files, number pages and maybe more...
MIT License
734 stars 156 forks source link

Is it possible to not save the combined PDF as a local file? #165

Closed TTD93 closed 5 years ago

TTD93 commented 5 years ago

Hi, I'm currently using combined PDF to read different files which is encoded in base64. After I've combined the PDF i want to upload it to storage using .attach(io:)

It seems like this gem forces you to save the file locally (and not as a Tempfile) before you can access the objects content? And is there an easier way to add the files to the combined PDF other than creating all the Tempfiles first?

My current code:

pdf = CombinePDF.new
strings_of_attachments_in_base_code64&.each do |string_attachment|
  tempfile = Tempfile.new
  File.open(tempfile, 'wb') do |f|
    f.write(Base64.decode64(string_attachment))
  end
  pdf << CombinePDF.load(tempfile.path)
end
pdf.save "#{Time.now.to_i}_combined_pdf.pdf"
boazsegev commented 5 years ago

Hi @TTD93 ,

Thank you for opening the issue.

I think the method you're looking for is #to_pdf.

This method formats the data to PDF a format and returns a binary string that represents the PDF file contents.

The contents can be saved to a file or uploaded as a binary blob.

Good luck! Bo.

TTD93 commented 5 years ago

Thanks!