如何在Rails中使用PDFKit来处理UTF-8编码?

8

我正在使用PDFKit生成我的Rails应用程序中的PDF。问题是我的一些内容包含非ASCII字符。我应该如何强制它使用UTF-8?

2个回答

15

通过将以下内容添加到HTML头部进行修复:

<meta http-equiv="Content-type" content="text/html; charset=utf-8" />

同样适用于Python的pdfkit。html = '<meta http-equiv="Content-type" content="text/html; charset=utf-8" />' + str(msg.html) - xjcl

3
You can write in PDFKIT configration.

 PDFKit.configure do |config|
        config.wkhtmltopdf = '/opt/wkhtmltopdf'
        config.default_options = {
          :encoding      => 'UTF-8'
          :page_size     => 'Letter',
          :margin_top    => '0.3in',          
          :margin_bottom => '0.1in',          
          :print_media_type => true                       
        }
 end

你能否添加一个链接,指向解释如何实现这个功能的文档? - ddor254
我已经在config/initializers/pdfkit.rb文件中编写了上述内容(ROR应用程序)。对我来说,它运行得很好。我是通过使用https://github.com/mileszs/wicked_pdf文档实现的。 - Govind shaw
请查看以下链接:https://stackoverflow.com/questions/55329282/how-to-load-images-precompiled-assets-with-pdfkit-wkhtmltopdf-rails-5-2 - Govind shaw
utf-8encoding 键的默认值,无论如何 https://github.com/pdfkit/pdfkit/blob/master/lib/pdfkit/configuration.rb#L19 - Artur INTECH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接