简体   繁体   English

如何使用prawn gem将Base64字符串转换为pdf文件

[英]How to convert Base64 string to pdf file using prawn gem

I want to generate pdf file from DB record. 我想从DB记录生成pdf文件。 Encode it to Base64 string and store it to DB. 将其编码为Base64字符串并将其存储到DB。 Which works fine. 哪个工作正常。 Now I want reverse action, How can I decode Base64 string and generate pdf file again? 现在我想要反向操作,如何解码Base64字符串并再次生成pdf文件?

here is what I tried so far. 这是我到目前为止所尝试的。

def data_pdf_base64
  begin
    # Create Prawn Object
    my_pdf = Prawn::Document.new
    # write text to pdf
    my_pdf.text("Hello Gagan, How are you?")
    # Save at tmp folder as pdf file
    my_pdf.render_file("#{Rails.root}/tmp/pdf/gagan.pdf")
    # Read pdf file and encode to Base64
    encoded_string = Base64.encode64(File.open("#{Rails.root}/tmp/pdf/gagan.pdf"){|i| i.read})
    # Delete generated pdf file from tmp folder
    File.delete("#{Rails.root}/tmp/pdf/gagan.pdf") if File.exist?("#{Rails.root}/tmp/pdf/gagan.pdf")
    # Now converting Base64 to pdf again
    pdf = Prawn::Document.new
    # I have used ttf font because it was giving me below error
    # Your document includes text that's not compatible with the Windows-1252 character set. If you need full UTF-8 support, use TTF fonts instead of PDF's built-in fonts.
    pdf.font Rails.root.join("app/assets/fonts/fontawesome-webfont.ttf")
    pdf.text Base64.decode64 encoded_string
    pdf.render_file("#{Rails.root}/tmp/pdf/gagan2.pdf")
  rescue => e
    return render :text => "Error: #{e}"
  end
end

Now I am getting below error: 现在我得到以下错误:

Encoding ASCII-8BIT can not be transparently converted to UTF-8. 编码ASCII-8BIT无法透明地转换为UTF-8。 Please ensure the encoding of the string you are attempting to use is set correctly 请确保正确设置您尝试使用的字符串的编码

I have tried How to convert base64 string to PNG using Prawn without saving on server in Rails but it gives me error: 我试过如何使用Prawn将base64字符串转换为PNG而不保存在Rails中的服务器上,但它给了我错误:

"\\xFF" from ASCII-8BIT to UTF-8 “\\ xFF”从ASCII-8BIT到UTF-8

Can anyone point me what I am missing? 任何人都可以指出我错过了什么吗?

The answer is to decode the Base64 encoded string and either send it directly or save it directly to disk (naming it as a PDF file, but without using prawn). 答案是解码Base64编码的字符串,直接发送或直接保存到磁盘(将其命名为PDF文件,但不使用prawn)。

The decoded string is a binary representation of the PDF file data, so there's no need to use Prawn or to re-calculate the content of the PDF data. 解码后的字符串是PDF文件数据的二进制表示,因此无需使用Prawn或重新计算PDF数据的内容。

ie

 raw_pdf_str = Base64.decode64 encoded_string
 render :text, raw_pdf_str # <= this isn't the correct rendering pattern, but it's good enough as an example.

EDIT 编辑

To clarify some of the information given in the comments: 澄清评论中提供的一些信息:

  1. It's possible to send the string as an attachment without saving it to disk, either using render text: raw_pdf_str or the #send_data method (these are 4.x API versions, I don't remember the 5.x API style). 可以将字符串作为附件发送而不将其保存到磁盘,使用render text: raw_pdf_str#send_data方法 (这些是4.x API版本,我不记得5.x API样式)。

  2. It's possible to encode the string (from the Prawn object) without saving the rendered PDF data to a file (save it to a String object instead). 可以对字符串进行编码(来自Prawn对象)而不将渲染的PDF数据保存到文件中(而是将其保存为String对象)。 ie: 即:

     encoded_string = Base64.encode64(my_pdf.render) 
  3. The String data could be used directly as an email attachment, similarly to the pattern provided here only using the String directly instead of reading any data from a file. String数据可以直接用作电子邮件附件,类似于此处提供的模式只使用String直接而不是从文件中读取任何数据。 ie: 即:

     # inside a method in the Mailer class attachments['my_pdf.pdf'] = { :mime_type => 'application/pdf', :content => raw_pdf_str } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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