简体   繁体   中英

How can I programmatically generate a PDF and save it on S3 using Carrierwave?

I am trying to generate a PDF using PDFKit

html = '<b>test</b>'
kit = PDFKit.new(html, :page_size => 'Letter')
pdf = kit.to_pdf

this works fine, and I can save the data to disk if I want to.

I have a simple model :

class Attachment < ActiveRecord::Base
  mount_uploader :file, FileUploader, mount_on: :filename
end

when I do :

a = Attachment.new
a.file = pdf

I receive the following error :

ArgumentError: string contains null byte

I prefer not to have to save the PDF to disk before uploading because I'll be using Heroku.

As the gist by William is not working for me, here is what I did to resolve the problem:

html = '<html><head></head><body>foo!</body></html>'
file = PDFKit.new html
file.to_pdf.gsub(/\0/, '')

The idea comes from the answer to this question . Side effects may apply.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM