Only the header of the pdfkit is output without interpreting the html tag.

Asked 2 years ago, Updated 2 years ago, 91 views

It implements the ability to generate PDF files using pdfkit.
The PDF file is ready.

https://github.com/pdfkit/pdfkit

However, the html tag appears as it is when you put in the common header and footer.

Controller processing is described as follows:

The layout was written in slim, and there was no problem with this part, and the html tag was interpreted.

def export
  response_to do | format |
    # Redirect when accessed with .html (in debug:1 to make show_as_html true)
    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"
      [email protected]_pdf,
        filename: "test.pdf",
        type: "application/pdf",
        disposition: "inline"
    end
  end
end

I added header_left and <p>hoge</p> is also output.
Only the header will have the html tag.

 mac OSX yosemite
pdfkit (0.8.2)
wicked_pdf (1.1.0)
Rails 4.2.7.1
ruby 2.3.1p112

I escaped, but it didn't work.

ApplicationController.helpers.sanitizes"<p>hoge</p>"

http://qiita.com/2or3/items/3f25216663190676a693

I thought Slim wouldn't work, so I did the following, but it didn't work.

@pdf=PDFKit.new('<html>head>/head>>body>>/body>>', header_left:'<p>hoge</p>>'/p>code

ruby-on-rails pdf

2022-09-30 19:51

1 Answers

When I searched header_left for pdfkit README and code, there was nothing to hit, so I thought pdfkit might be an acceptable argument.

PDFKit.new(html,header_left:'<p>hoge</p>')

html=render_to_string template: 'layouts/export.html.slim'

How about writing <p>hoge</p> in export.html.slim?


2022-09-30 19:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.