grails-wkhtmltopdf plugin is not rendering - grails

Grails 3.0.9
compile "org.grails.plugins:wkhtmltopdf:1.0.0.RC3"
application.yml already has pdf: application/pdf
command line works %> wkhtmltopdf www.google.com myhomepage.pdf
I am trying to generate PDF in controller/action. But it renders the template rather than generate PDF.
render( filename:"File bla.pdf",
view:"/sr/pdfGenerator",
model:[bla:bla],
header:"",
footer:"",
marginLeft:20,
marginTop:35,
marginBottom:20,
marginRight:20,
headerSpacing:10)
If i comment out render() from action it will generate empty PDF.

This seems to be a bug in the introduced because of a change after Grails 3.0.7. Please see developer response in GitHub

Related

Rails PDFKit - Errno::ENOENT (No such file or directory) when using to_file

Whenever I try to generate a pdf using to_file, the process will just hang, and when I stop the development server I get Errno::ENOENT (No such file or directory - path/to/pdf). However, I am able to render a pdf inline using to_pdf. I'm also able to generate PDFs from the command line in the same folder that I'm trying to generate them in with to_file.
I'm using Rails 3.2.12 and pdfkit 0.8.2. I've tried using wkhtmltopdf versions 0.9.6 through 0.12.4. I'm on Ubuntu 14.04.
Example from controller:
html = render_to_string(:action => "show.html.erb", :formats => :html)
kit.stylesheets << "{Rails.root}/app/assets/stylesheets/stylesheet1.css"
kit.stylesheets << "#{Rails.root}/vendor/assets/stylesheets/stylesheet2.css"
kit.to_file("#{Rails.root}/folder_to_write_to/generated_pdf.pdf")
Turns out the issue was the asset pipeline conflicting with wkhtmltopdf. Added config.threadsafe! to development.rb and it started working.
Another issue can be the default options passed. For example, when I left the default print_media_type option in place, found this message in the log:
The switch --print-media-type, is not support using unpatched qt, and will be ignored."
Only when I override that does it work for me, either in the initializer or like so:
PDFKit.new(html, {print_media_type: false})
The message says it'll be ignored, but it wasn't. It was causing the file to not get generated.

PDFKit header or footer not working

I am trying to add page numbers to each page of pdf generated using PdfKit. The following is my code :
content = File.read( "report.html.erb")
template = ERB.new(content)
set_margin = 0.to_s
kit = PDFKit.new(template.result(binding), :header_center => "Page [page] of [toPage]", page_width: '157.42', page_height: '52.77', :margin_top => set_margin+'in', :margin_right => set_margin+'in', :margin_bottom => set_margin+'in', :margin_left => set_margin+'in')
kit.to_file(file_path)
No header is getting displayed on the generated pdf. Please provide a solution for this.
Please type wkhtmltopdf --help in console. I believe, you have not patched Qt library with wkhtmltopdf fixes, hence you will see at the bottom of output:
Reduced Functionality:
This version of wkhtmltopdf has been compiled against a version of QT without
the wkhtmltopdf patches. Therefore some features are missing, if you need
these features please use the static version.
Currently the list of features only supported with patch QT includes:
Printing more then one HTML document into a PDF file.
Running without an X11 server.
Adding a document outline to the PDF file.
Adding headers and footers to the PDF file.
Generating a table of contents.
Adding links in the generated PDF file.
Printing using the screen media-type.
Disabling the smart shrink feature of webkit.
To print out headers and footers one should rebuild Qt library with patches provided by wkhtmltopdf.

phantomjs: How to run phantomjs command when Url with queryparams

I am using Phantomjs to generate pdf from web page.
Using following command to generate pdf:
`phantomjs rasterize.js http://www.myurl.com?q=123&&selected_opts%5B%5D=fs&selected_opts%5B%5D=disc form1.pdf`
where, selected_opts is array type
It is giving me error - form1.pdf: not found or selected_opts%5B%5D=fs: not found
If I remove querystring from url then it generate pdf, any idea how to run phantomjs having url with querystring.
You need to protect all & chars with a backslash :
phantomjs rasterize.js http://www.myurl.com?q=123\&s=foo form1.pdf
Ampersand is annoying in console so you need to protect it like above. In a ruby script you need to protect backslash that protect & :
`phantomjs rasterize.js http://www.myurl.com?q=123\\&s=foo form1.pdf`
example :
`phantomjs rasterize.js https://www.google.fr/search?q=rasterize.js+special+char\\&oq=rasterize.js google1.pdf`

Render SVG Tag in PDF

I want to render a signature in a pdf.
my html code:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<path stroke="navy" stroke-width="2" fill="none" d="<%= #image %>"/>
</svg>
#image = "M96,8 L96,9 L96,11 L96,14 L98,18 L101,22 L115,32 L126,43
L142,50 L165,56 L189,56 L207,56 L219,50 L230,46 L233,43 L235,41
L236,40 L238,39 L239,39"
how can I render this in pdf?
im using rails 3 + prawn.
OR how can i create with #image an png?
any advice?
I am not familiar with ruby on rails or prawn but if you look at the Graphics section of the PDF spec, specifically in the path section it shows how the commands are laid out.
You could potentially use this info to map from the SVG draw commands to the postscript/pdf draw commands.
A question that might help: How to insert a SVG file in a PDF document?
You can use the wicked_pdf gem which renders HTML and JavaScript into a PDF file. It uses the wkhtmltopdf lib.
To convert to PNG I would recommend the IMGKit gem, which uses wkhtmltoimage lib.
You can use https://github.com/mogest/prawn-svg
Can't confirm if it works, but as I needed to do the same as you.
I've found that after a simple google research I found this plugin.
I will test it as soon as I can.
Kind regards

I can't get rails plugin wicked_pdf to work

I wanted to create PDFs for my rails application using wkhtml2pdf and wicked_pdf.
I downloaded and extracted wkhtml2pdf beta 4 and placed it in /usr/local/bin/wkhtml2pdf
I tried running it on a web site and it gave a nice result.
In my rails application (2.3.4) I installed wicked_pdf:
script/plugin install git://github.com/mileszs/wicked_pdf.git
script/generate wicked_pdf
Everything seemed to be ok.
inside script/console I run the following - (with the following output)
wp = WickedPdf.new
=># WickedPdf:0xb62f2c70 #exe_path="/usr/local/bin/wkhtmltopdf"
HTML_DOCUMENT = "<html><body>Hello World</body></html>"
=> "<html><body>Hello World</body></html>"
pdf = wp.pdf_from_string HTML_DOCUMENT
=> "/usr/local/bin/wkhtmltopdf - - -q"
=> "\n\n\n\n\n\n\n\n\n\n"
of course this isn't good. According to the test the result of my last command should start with "%pdf-1.4"
Any idea what I can do?
Having the same problem. Removed the -q option from the wicked_pdf.rb file on line 19 and then was able to get the proper string on the console.
=> "%PDF-1.4\n1 0 obj\n<<\n/Title ...
This also seems to have solved other problems. The PDF still didn't render correctly when using it from the web site - embedded font issue - on to the next issue now.
Hopefully this will work for you.

Resources