我想从DB记录生成pdf
文件。将其编码为Base64
字符串并将其存储到数据库。哪些工作正常。现在我想要反向操作,如何解码Base64
字符串并再次生成pdf
文件?如何将Base64字符串转换为使用大虾宝石的pdf文件
这是我到目前为止所尝试的。
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
现在我得到以下错误:
Encoding ASCII-8BIT can not be transparently converted to UTF-8. Please ensure the encoding of the string you are attempting to use is set correctly
我试图How to convert base64 string to PNG using Prawn without saving on server in Rails,但它给我的错误:
"\xFF" from ASCII-8BIT to UTF-8
任何人都可以指向我,我缺少的是什么?
@Med:OK,我们来试试将更新你很快 –
@Med:收到此错误:'无效字节顺序UTF-8' –
你的问题还不清楚。首先你说你在数据库中存储了一个PDF文件。然后你问你如何从数据库中的数据生成一个PDF文件。但你只是说数据*是一个PDF文件!那么,这是什么? –