pdfkitを用いて、PDFファイルを生成する機能を実装しておりまして、
PDFファイルができるところまではできました。
https://github.com/pdfkit/pdfkit
ただ、共通のheader, footerを入れる際に、htmlタグがそのままでてしまいます。
Controllerの処理は次の通り、記述しております。
レイアウトはslimで書いておりまして、この部分は問題なく、htmlタグが解釈できておりました。
def export respond_to do |format| # .html でアクセスした際は、リダイレクト(show_as_html を true にするため、debug: 1 で) format.html { redirect_to action: 'export', format: 'pdf', debug: 1 } format.pdf do html = render_to_string template: 'layouts/export.html.slim' @pdf = PDFKit.new(html, header_left: '<p>hoge</p>') @pdf.stylesheets << "#{Rails.root}/app/assets/stylesheets/pdf.css" send_data @pdf.to_pdf, filename: "test.pdf", type: "application/pdf", disposition: "inline" end end end
header_leftを追加し <p>hoge</p>
も出力されているのですが、
headerだけはhtmlタグがそのままでてしまいます。
実行環境
mac OSX yosemite pdfkit (0.8.2) wicked_pdf (1.1.0) Rails 4.2.7.1 ruby 2.3.1p112
対策1
エスケープしたりしましたが、ダメでした。
ApplicationController.helpers.sanitize "<p>hoge</p>"
http://qiita.com/2or3/items/3f25216663190676a693
対策2
Slimがダメなのかと思い次の通りやりましたが、ダメでした。
@pdf = PDFKit.new('<html><head></head><body></body></html>', header_left: '<p>hoge</p>')
デバッグ時のインスタンス変数の中身
> @pdf => #<PDFKit:0x007ffad89d2c60 @host_is_windows=false, @options= {"--encoding"=>"UTF-8", "--page-size"=>"A4", "--margin-top"=>"0.25in", "--margin-right"=>"1in", "--margin-bottom"=>"0.25in", "--margin-left"=>"1in", "--header-left"=>"<p>hoge</p>"}, @source=#<PDFKit::Source:0x007ffad89d2c38 @is_url=nil, @source="<html><head></head>">, @stylesheets=["path/to/pdf.css"]>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/03 06:29
2017/05/03 06:37 編集
2017/05/03 06:41
2017/05/03 06:42
2017/05/03 06:56
2017/05/03 07:02
2017/05/03 07:06
2017/05/03 07:07