Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert html to pdf on the fly using Python and HTMLDOC
    primarykey
    data
    text
    <p>I built a Django application for a client about a year ago. He has now resold the application to some super secret government agency that they won't even tell me the name of. </p> <p>Part of the application dynamically generates PDF files using the python library xhtml2pdf (pisa). The Government doesn't like this library, they won't tell me why, they said I have to use <a href="https://www.msweet.org/projects.php?Z1" rel="nofollow">HTMLDOC</a> for pdf generation.</p> <p>There's not much documentation on this library, but from reading the PHP example, it looks like you can just communicate with it through the shell, so it should work with Python. However, I'm having a hard time passing the html to HTMLDOC. It looks like HTMLDOC will only accept a file, but I really need to pass the html as a string since it's dynamically generated. (Or write the html string to a temporary file and then pass that temporary file to HTMLDOC).</p> <p>I thought StringIO would work for this, but I'm getting an error. Here's what I have:</p> <pre><code>def render_to_pdf(template_src, context_dict): template = get_template(template_src) context = Context(context_dict) html = template.render(context) result = StringIO.StringIO(html.encode("utf-8")) os.putenv("HTMLDOC_NOCGI", "1") #this line throws "[Errno 2] No such file or directory" htmldoc = subprocess.Popen("htmldoc -t pdf --quiet '%s'" % result, stdout=subprocess.PIPE).communicate() pdf = htmldoc[0] result.close() return HttpResponse(pdf, mimetype='application/pdf') </code></pre> <p>Any ideas, tips, or help would be really appreciated.</p> <p>Thanks.</p> <p><strong>UPDATE</strong></p> <p>Stack Trace:</p> <pre><code>Environment: Request Method: GET Request URL: (redacted) Django Version: 1.3 alpha 1 SVN-14921 Python Version: 2.6.5 Installed Applications: ['django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.admin', 'application'] Installed Middleware: ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware') Traceback: File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view 23. return view_func(request, *args, **kwargs) File "/home/ascgov/application/views/pdf.py" in application_pdf 90. 'user':owner}) File "/home/ascgov/application/views/pdf.py" in render_to_pdf 53. htmldoc = subprocess.Popen("/usr/bin/htmldoc -t pdf --quiet '%s'" % result, stdout=subprocess.PIPE).communicate() File "/usr/lib/python2.6/subprocess.py" in __init__ 633. errread, errwrite) File "/usr/lib/python2.6/subprocess.py" in _execute_child 1139. raise child_exception Exception Type: OSError at /pdf/application/feed-filtr/ Exception Value: [Errno 2] No such file or directory </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload