简体   繁体   中英

wicked_pdf doesn't work — Ruby on Rails

I want to generate PDF from a html doc in a rails app. So I decided to use wicked_pdf. But its not working. I tried to integrate it in an app, but it did not work. So I thought of trying to see if it works in the rails console then i will integrate it in the app.

Here's what I came across in the console

wp = WickedPdf.new
=> #<WickedPdf:0x4e7eea0 @exe_path="C:\\wkhtmltopdf\\wkhtmltopdf.exe">
HTML_DOCUMENT = "<html><body>Hello World</body></html>"
=> "<html><body>Hello World</body></html>"
pdf = wp.pdf_from_string HTML_DOCUMENT
"***************C:\\wkhtmltopdf\\wkhtmltopdf.exe  - - ***************"
NotImplementedError: fork() function is unimplemented on this machine
    from C:/Ruby/lib/ruby/1.8/open3.rb:57:in `fork'
    from C:/Ruby/lib/ruby/1.8/open3.rb:57:in `popen3'
    from C:/Users/raw/Desktop/html/scheduler/vendor/plugins/wicked_pdf/lib/wicked_pdf.rb:22:in `pdf_from_string'
    from (irb):3

UPDATE: I am using Windows7

I am not able to figure out where I am going wrong.

Please help.

Thanks in advance.

Install the gem win32-open3

gem install win32-open3

And now Change line:6 in lib/wicked_pdf.rb

require 'open3'

to

require 'win32/open3'

and line:20 in the same file

command_for_stdin_stdout = "#{@exe_path} #{options} - - -q" 

to

command_for_stdin_stdout = "#{@exe_path} #{options} - -" 

and now in console I get

wp = WickedPdf.new
=> #<WickedPdf:0x4e65f70 @exe_path="C:\\wkhtmltopdf\\wkhtmltopdf.exe">

?> HTML_DOCUMENT = "<html><body>Hello World</body></html>"
=> "<html><body>Hello World</body></html>"

?> pdf = wp.pdf_from_string HTML_DOCUMENT
"***************C:\\wkhtmltopdf\\wkhtmltopdf.exe  - - ***************"
=> "%PDF-1.4\n1 0 obj\n<<\n/Title (\376\377)\n/Producer (wkhtmltopdf)\n/CreationDate (D:20101127124137)\n>>\nendobj\n4 0 obj\n<<\n/Type /ExtGState\n/SA true\n/SM 0.02\n/ca 1.0\n/CA 1.0\n/AIS false\n/SMask /None>>\nendobj\n5 0 obj\n[/Pattern /DeviceRGB]\nendobj\n8 0 obj\n<<\n/Type /Catalog\n/Pages 2 0 R\n>>\nendobj\n6 0 obj\n<<\n/Type /Page\n/Parent 2 0 R\n/Contents 9 0 R\n/Resources 11 0 R\n/Annots 12 0 R\n/MediaBox [0 0 595 842]\n>>\nendobj\n11 0 obj\n<<\n/ColorSpace <<\n/PCSp 5 0 R\n/CSp /DeviceRGB\n/CSpg /DeviceGray\n>>\n/ExtGState <<\n/GSa 4 0 R\n>>\n/Pattern <<\n>>\n/Font <<\n/F7 7 0 R\n>>\n/XObject <<\n>>\n>>\nendobj\n12 0 obj\n[ ]\nendobj\n9 0 obj\n<<\n/Length 10 0 R\n/Filter /FlateDecode\n>>\nstream\nx\234\245\222QKÄ0\f\200ßó+ò,\×6ëÖ‚øàPÁa\254àƒø ;O9\274Ãy\017\376}ÓtÇ�\003\213`ÖåKÒ\264ɪ\273áß\016XuÃ'\216\263îÐJ\273\220\027j\226ÕX\257Èê\264ÐRM+tÜÁ\204\023ôÐó;é Ž\273j\221ø\207*×›\243\022Ý�\017\r)ÞÁ\023\233\037K\223È©`\264v\236\271>7Sð;<^à¾Xêè1\"KÏA\271ÐÖ\244\255\225\2643\223o$\224j$\207_\257\260IçþKÚï\247Édèøë-Þó\263ŧg\206ëâ5\256#T\267-\032\217qÃ÷\220\241d\025\271\205\270\2625Æ5^òPÌÆ-\230䜉\025\022N\200\004P\001Ôç)N@}\002M.ã
9\255\000W(ãÜDil\376qLî¢ùo3Ó ÊØÃ\0177Ч\201endstream\nendobj\n10 0 obj\n271\nendobj\n13 0 obj\n<< /Type /FontDescriptor\n/FontName /QNAAAA+ArialRegular\n/Flags 4 \n/FontBBox [-594.726562 -290.527343 1790.03906 900.390625 ]\n/ItalicAngle 0 \n/Ascent 651.367187 \n/Descent -188.476562 \n/CapHeight 0 \n/StemV 65.4296875 \n/FontFile2 14 0 R\n>> endobj\n14 0 obj\n<<\n/Length1 3840 \n/Length 17 0 R\n/Filter /FlateDecode\n>>\nstream\nx\234\245W]l\024×>wgg×C0ÆkW\204p\rÄ?\024l\263Æ»ÆÄ8ÈD\245\030cc\e\0220\224\214wf\275cvgV\263\263^\257\241\205\2406BQ#\245\205\246\025êC‰\2226I[~ÒÚ”\246\250M\245<\264\225ÒH\225\252\242ÊM\213\232\206(U\037\"\265Ä?=÷ÜYgL(RÕ]Í�ïœ{î9ßùæÞY\030\000\224ÁiP\000ú‡Z¢+\242Gsèù*^O\216\245\213Ék\237Áø

Take a look at this blog post for understanding it. Thanks.

You can try DocRaptor.com. It's an online app that lets you convert html to pdf . They have free accounts available, plus paid accounts if you need a lot of docs.

Hope that helps!

Julie

create a folder in your public called "pdfs"

Controller:

pdf = render_to_string :pdf => "graphs", :orientation => 'Landscape', :font_size => 11
# then save to a file
save_path = Rails.root.join('pdfs', filename.pdf')
File.open(save_path, 'wb') do |file|
file << pdf
end

graphs.rhtml

View: add this line of code for styles

<%= wicked_pdf_stylesheet_link_tag "css filename here" %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM