Wicked_pdf 生成带有奇怪字符的 PDF,但是当我使用控制器渲染时一切都很好
Wicked_pdf generate my PDF with strange characters, but when I use the Controller render everything is quite good
我是 wicked_pdf gem 的新手,但我注意到生成 PDF 时有些奇怪,我使用 gem 有两个目的,一个是显示引文第二个通过电子邮件发送 PDF。
在 Controller 的 show action 中,我有这样的东西:
def show
@lot_quotation = set_lot_quotation
authorize @lot_quotation
respond_to do |format|
format.html
format.pdf do
filename = 'cotizacion_' + @lot_quotation.id.to_s
render pdf: "lot_quotation",
layout: 'lot_quotation.html.erb',
page_size: 'Letter',
save_to_file: Rails.root.join('pdfs', "#{filename}.pdf")
end
end
end
一切都很棒!
然后我有一个动作 "send_quotation",生成 PDF,然后我使用 ActionMailer 发送它...这是我的代码:
def send_quotation
@lot_quotation = LotQuotation.find(params[:id])
filename = 'cotizacion_' + @lot_quotation.id.to_s + '.pdf'
html = render_to_string template: 'lot_quotations/show.pdf.erb', layout: "lot_quotation.html", pdf: "lot_quotation",
page_size: "Letter", encoding: 'UTF-8'
pdf = WickedPdf.new.pdf_from_string(html)
# then save to a file
save_path = Rails.root.join('pdfs',filename)
File.open(save_path, 'wb') do |file|
file << pdf
end
SeruMailer.cotizacion(params[:email_to], @lot_quotation)
authorize @lot_quotation
respond_to do |format|
format.html
format.js
end
end
它有效,但是...当我打开 pdf 文件时,我有这样的东西:
%PDF-1.4 1 0 obj << /Title (þÿCotizacion) /Creator (þÿwkhtmltopdf 0.12.2.1) /Producer (þÿQt 4.8.6) /CreationDate (D:20150508165111-04'00') >> endobj 3 0 obj <<
/Type /ExtGState /SA true /SM 0.02 /ca 1.0 /CA 1.0 /AIS false /SMask /None>> endobj 4 0 obj [/Pattern /DeviceRGB] endobj 6 0 obj << /Type /XObject /Subtype /Image
/Width 351 /Height 69 /BitsPerComponent 8 /ColorSpace /DeviceRGB /Length 7 0 R /Filter /DCTDecode >> stream ÿØÿàJFIF``ÿÛC
ÿÛC ÿÀ E _ " ÿÄ
ÿÄμ } !1A Qa "q 2 ‘¡ #B±Á RÑðbr‚ %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„... †‡ˆ‰Š’“”•–— ̃TMš¢£¤¥¦§ ̈©a23 ́μ¶· ̧1oÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ
ÿÄμ w !1 AQ aq "2 B‘¡±Á#3Rð brÑ á%ñ &'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz‚ƒ„... †‡ˆ‰Š’“”•–— ̃TMš¢£¤¥¦§ ̈©a23 ́μ¶· ̧1oÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚâãäåæçèéêòóôõö÷øùúÿÚ ?ýü¢Šà~'þÒžøC þÜÖí#Ÿμ
́N$ ÿà ßUMWW¶Ðt÷o1⁄4 ̧‚Ö ÇÏ$® ñ5ñ ̄Å¿ø* ÕÜ/oàí -
éúüïtÿr>õóWÄ Œþ*ø§xòëoÖ¡ ̈ Ñ ÿwÿ|P Ý_ ÿà¡Þ øy Ä t× %Ô |‘Y ûmÒ1⁄4oÃßðTÝuÿ† ̈ðN1ÿãIÿ¿0 ñ ÙþÍ3⁄4 OˆŸ2Ýî ,2[%ý
我不知道为什么?我错过了什么吗?我已经检查了 github 问题、问题和类似的东西...但我没有找到任何线索...我认为与 render_to_string.
有关
编辑
这是我的布局app/views/layouts/lot_quotation。html.erb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta charset="utf-8" />
<%= wicked_pdf_stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= wicked_pdf_javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<style>
body {font-size: 11px !important;}
h3 {font-size: 12px !important;}
table, tr, td, th, tbody, thead, tfoot {
page-break-inside: avoid !important;
}
h2 {font-size: 14px !important;}
</style>
</head>
<title>Cotizacion</title>
<body>
<%= yield %>
</body>
</html>
如果您只想渲染 pdf,这段代码就足够了:
def show
respond_to do |format|
format.html
format.pdf do
render pdf: 'file_name',
template: 'orders/order_success_new_design.pdf.erb',
layout: 'layouts/application_new.pdf.erb'
end
end
end
如果要另存为pdf:
def save
pdf = WickedPdf.new.pdf_from_string(
render_to_string(
template: 'example/pdf_view.pdf.erb',
layout: 'layouts/application.pdf.erb'))
send_data(pdf,
filename: 'file_name.pdf',
type: 'application/pdf',
disposition: 'attachment')
end
我是 wicked_pdf gem 的新手,但我注意到生成 PDF 时有些奇怪,我使用 gem 有两个目的,一个是显示引文第二个通过电子邮件发送 PDF。
在 Controller 的 show action 中,我有这样的东西:
def show
@lot_quotation = set_lot_quotation
authorize @lot_quotation
respond_to do |format|
format.html
format.pdf do
filename = 'cotizacion_' + @lot_quotation.id.to_s
render pdf: "lot_quotation",
layout: 'lot_quotation.html.erb',
page_size: 'Letter',
save_to_file: Rails.root.join('pdfs', "#{filename}.pdf")
end
end
end
一切都很棒!
然后我有一个动作 "send_quotation",生成 PDF,然后我使用 ActionMailer 发送它...这是我的代码:
def send_quotation
@lot_quotation = LotQuotation.find(params[:id])
filename = 'cotizacion_' + @lot_quotation.id.to_s + '.pdf'
html = render_to_string template: 'lot_quotations/show.pdf.erb', layout: "lot_quotation.html", pdf: "lot_quotation",
page_size: "Letter", encoding: 'UTF-8'
pdf = WickedPdf.new.pdf_from_string(html)
# then save to a file
save_path = Rails.root.join('pdfs',filename)
File.open(save_path, 'wb') do |file|
file << pdf
end
SeruMailer.cotizacion(params[:email_to], @lot_quotation)
authorize @lot_quotation
respond_to do |format|
format.html
format.js
end
end
它有效,但是...当我打开 pdf 文件时,我有这样的东西:
%PDF-1.4 1 0 obj << /Title (þÿCotizacion) /Creator (þÿwkhtmltopdf 0.12.2.1) /Producer (þÿQt 4.8.6) /CreationDate (D:20150508165111-04'00') >> endobj 3 0 obj <<
/Type /ExtGState /SA true /SM 0.02 /ca 1.0 /CA 1.0 /AIS false /SMask /None>> endobj 4 0 obj [/Pattern /DeviceRGB] endobj 6 0 obj << /Type /XObject /Subtype /Image
/Width 351 /Height 69 /BitsPerComponent 8 /ColorSpace /DeviceRGB /Length 7 0 R /Filter /DCTDecode >> stream ÿØÿàJFIF``ÿÛC
ÿÛC ÿÀ E _ " ÿÄ
ÿÄμ } !1A Qa "q 2 ‘¡ #B±Á RÑðbr‚ %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„... †‡ˆ‰Š’“”•–— ̃TMš¢£¤¥¦§ ̈©a23 ́μ¶· ̧1oÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ
ÿÄμ w !1 AQ aq "2 B‘¡±Á#3Rð brÑ á%ñ &'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz‚ƒ„... †‡ˆ‰Š’“”•–— ̃TMš¢£¤¥¦§ ̈©a23 ́μ¶· ̧1oÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚâãäåæçèéêòóôõö÷øùúÿÚ ?ýü¢Šà~'þÒžøC þÜÖí#Ÿμ
́N$ ÿà ßUMWW¶Ðt÷o1⁄4 ̧‚Ö ÇÏ$® ñ5ñ ̄Å¿ø* ÕÜ/oàí -
éúüïtÿr>õóWÄ Œþ*ø§xòëoÖ¡ ̈ Ñ ÿwÿ|P Ý_ ÿà¡Þ øy Ä t× %Ô |‘Y ûmÒ1⁄4oÃßðTÝuÿ† ̈ðN1ÿãIÿ¿0 ñ ÙþÍ3⁄4 OˆŸ2Ýî ,2[%ý
我不知道为什么?我错过了什么吗?我已经检查了 github 问题、问题和类似的东西...但我没有找到任何线索...我认为与 render_to_string.
有关编辑
这是我的布局app/views/layouts/lot_quotation。html.erb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta charset="utf-8" />
<%= wicked_pdf_stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= wicked_pdf_javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<style>
body {font-size: 11px !important;}
h3 {font-size: 12px !important;}
table, tr, td, th, tbody, thead, tfoot {
page-break-inside: avoid !important;
}
h2 {font-size: 14px !important;}
</style>
</head>
<title>Cotizacion</title>
<body>
<%= yield %>
</body>
</html>
如果您只想渲染 pdf,这段代码就足够了:
def show
respond_to do |format|
format.html
format.pdf do
render pdf: 'file_name',
template: 'orders/order_success_new_design.pdf.erb',
layout: 'layouts/application_new.pdf.erb'
end
end
end
如果要另存为pdf:
def save
pdf = WickedPdf.new.pdf_from_string(
render_to_string(
template: 'example/pdf_view.pdf.erb',
layout: 'layouts/application.pdf.erb'))
send_data(pdf,
filename: 'file_name.pdf',
type: 'application/pdf',
disposition: 'attachment')
end