我想从数据库记录中生成
以下是我目前尝试的方法。
现在我遇到以下错误:
编码ASCII-8BIT无法透明地转换为UTF-8。请确保您尝试使用的字符串的编码已正确设置。
我尝试了如下方法如何在Rails中使用Prawn将Base64字符串转换为PNG而无需保存到服务器,但它给出了以下错误:
从ASCII-8BIT到UTF-8的"\xFF"。
有人能指出我漏掉了什么吗?
pdf
文件,将其编码为 Base64
字符串并存储到数据库中。 这个过程是正常的。 现在我想进行相反的操作,如何解码 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
现在我遇到以下错误:
编码ASCII-8BIT无法透明地转换为UTF-8。请确保您尝试使用的字符串的编码已正确设置。
我尝试了如下方法如何在Rails中使用Prawn将Base64字符串转换为PNG而无需保存到服务器,但它给出了以下错误:
从ASCII-8BIT到UTF-8的"\xFF"。
有人能指出我漏掉了什么吗?