Convert html to pdf using prawn in ruby on rails

1.4k Views Asked by At

I am working on a Rails project and I need to convert the HTML page to a PDF page but it's writing HTML as it is on a pdf page. PDF page is not showing like a webpage. How can I generate a proper PDF from an HTML file?

Prawn::Document.generate("test.pdf") do
  filepath = ${filepath}"file.html"
  data = File.read(filepath);
  text data  
end
1

There are 1 best solutions below

0
eugen On

prawn is not really an HTML to PDF generator - see https://github.com/prawnpdf/prawn#should-you-use-prawn

You'll need to use another tool, for example wicked_pdf - see https://github.com/mileszs/wicked_pdf#super-advanced-usage

In your case, to quote from the README, you'll need something like

# create a pdf file from a html file without converting it to string
# Path must be absolute path
pdf = WickedPdf.new.pdf_from_html_file('/your/absolute/path/here')