# Generating a PDF using popen and wkhtmltopdf

**URL:** <https://www.ruby-forum.com/t/generating-a-pdf-using-popen-and-wkhtmltopdf/182739>\
**Category:** Rails\
**Created:** [February 2, 2010, 5:14am UTC](https://www.ruby-forum.com/t/generating-a-pdf-using-popen-and-wkhtmltopdf/182739 "2010-02-02T05:14:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nrimbeau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f14d63/32.png) [@nrimbeau](https://www.ruby-forum.com/u/nrimbeau)\
**Post date:** [February 2, 2010, 5:14am UTC](https://www.ruby-forum.com/t/generating-a-pdf-using-popen-and-wkhtmltopdf/182739/1 "2010-02-02T05:14:57Z")

</div>

As described on wkhtmltopdf Google group  
([Google Code Archive - Long-term storage for Google Code Project Hosting.](http://code.google.com/p/wkhtmltopdf/issues/detail?id=162)), I have a  
problem generating a PDF while using popen and wkhtmltopdf.

wkhtmltopdf takes HTML code as input and ouputs a PDF file. Here is what  
I’m doing:

```
command = '"C:\Program Files\wkhtmltopdf\wkhtmltopdf.exe" - - -q'
IO.popen(command, 'r+') do |f|

  # Writing the html previously rendered in a string
  f.write(html_output)
  f.close_write

  # Reading the ouput and closing
  pdf = f.readlines
  f.close

  # Returning the pdf data
  pdf

end

```

This code results in a corrupted PDF file. I checked the PDF itself  
which shows some differences with a valid PDF file, like some missing  
closing tags (endstream) - but I’m not an expert of that format.

Well, my question is the following: am I doing it wrong, using a wrong  
method, missing something, or wkhtmltopdf is more likely to be the  
problem?

I attached the corrupted file.

If you have a look at it, you’ll notice that a PDF EOF symbol is there,  
which tends to say that the generation was not interrupted in any way.

Any idea?

Thanks for your help!

- Nicolas

---

<div class="post-metadata">

**Author:** ![nrimbeau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f14d63/32.png) [@nrimbeau](https://www.ruby-forum.com/u/nrimbeau)\
**Post date:** [February 2, 2010, 8:46pm UTC](https://www.ruby-forum.com/t/generating-a-pdf-using-popen-and-wkhtmltopdf/182739/2 "2010-02-02T20:46:36Z")

</div>

Hi Nicolas,

Whenever I generate pdfs from a rails app using wkhtmltopdf (or  
princexml), I usually call wkhtmltopdf using an app\_url (ie  
wkhtmltopdf hits the web app to get the html/css/imgs/… to be used  
to gen the pdf), something like the following:

# in some controller …

require ‘timeout’  
…

TIMEOUT\_SECS = 5  
…

def gen\_pdf  
url\_to\_pdf = … # the url to gen the pdf from.  
fname = … # the name of the resulting pdf.  
ftype = “application/pdf”  
# combat shell injection?  
app\_url = app\_url.to\_s.gsub(/["’\s`$;><&\|\(\)\\\[\]]/, '') s = nil # valid url? unless (app_url =~ URI::regexp).nil? begin timeout(TIMEOUT_SECS) do # gen pdf from url. s =`wkhtmltopdf -q “#{app\_url}” -`.chomp  
end  
rescue Exception =\> e  
… # log, render/redirect err msg, …  
end  
end  
# invalid pdf?  
if not s.to\_s =~ /^%PDF/  
… # log, render/redirect err msg, …  
end

```
send_data(s, :type=>ftype, :filename=>fname); return

```

end  
…

Jeff

---

<div class="post-metadata">

**Author:** ![nrimbeau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f14d63/32.png) [@nrimbeau](https://www.ruby-forum.com/u/nrimbeau)\
**Post date:** [February 2, 2010, 9:09pm UTC](https://www.ruby-forum.com/t/generating-a-pdf-using-popen-and-wkhtmltopdf/182739/3 "2010-02-02T21:09:27Z")

</div>

(reposted due to typo… )

Hi Nicolas,

Whenever I generate pdfs from a rails app using wkhtmltopdf (or  
princexml), I usually call wkhtmltopdf using an app\_url (ie  
wkhtmltopdf hits the web app to get the html/css/imgs/… to be used  
to gen the pdf), something like the following:

# in some controller …

require ‘timeout’  
…  
TIMEOUT\_SECS = 5  
…  
def gen\_pdf  
app\_url = … # the url to gen the pdf from.  
fname = … # the name of the resulting pdf.  
ftype = “application/pdf”  
# combat shell injection?  
app\_url = app\_url.to\_s.gsub(/["’\s`$;><&\|\(\)\\\[\]]/, '') s = nil # valid url? unless (app_url =~ URI::regexp).nil? begin timeout(TIMEOUT_SECS) do # gen pdf from url. s =`wkhtmltopdf -q “#{app\_url}” -`.chomp  
end  
rescue Exception =\> e  
… # log, render/redirect err msg, …  
end  
end  
# invalid pdf?  
if not s.to\_s =~ /^%PDF/  
… # log, render/redirect err msg, …  
end  
send\_data(s, :type=\>ftype, :filename=\>fname); return  
end  
…

Jeff

---

<div class="post-metadata">

**Author:** ![nrimbeau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f14d63/32.png) [@nrimbeau](https://www.ruby-forum.com/u/nrimbeau)\
**Post date:** [February 3, 2010, 2:42am UTC](https://www.ruby-forum.com/t/generating-a-pdf-using-popen-and-wkhtmltopdf/182739/4 "2010-02-03T02:42:02Z")

</div>

Thanks for your answer Jeff. I’ll give it a try in my own app and see  
whether it’s working or not. I’ll keep you posted!

Cheers,

- Nicolas
