Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7074459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:03:37+00:00 2026-05-28T06:03:37+00:00

I built a Django application for a client about a year ago. He has

  • 0

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.

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 HTMLDOC for pdf generation.

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).

I thought StringIO would work for this, but I’m getting an error. Here’s what I have:

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')

Any ideas, tips, or help would be really appreciated.

Thanks.

UPDATE

Stack Trace:

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:03:38+00:00Added an answer on May 28, 2026 at 6:03 am

    First, subprocess.Popen‘s first arg should generally be a list (unless you also pass shell=True). The No such file or directory is almost certainly caused by the absence of a file named "htmldoc -t pdf --quiet '... on the system (it’s trying to find and run the program named for the whole string value).

    Second, if you give htmldoc some html on its stdin, it’ll spit out a pdf on its stdout, thus avoiding the need for a temporary file.

    Give this a try (untested):

    htmldoc = subprocess.Popen(
      ['/usr/bin/htmldoc', '-t', 'pdf', '--webpage', '-'], 
      stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
    )
    stdout, stderr = htmldoc.communicate(html)
    

    NB: substitute /usr/bin/htmldoc for the real path to htmldoc on your system.

    The - argument to the htmldoc program, tells it to read from stdin. You’ll pass your html string value (html) to htmldoc’s stdin as an argument to the htmldoc.communicate call. The resulting pdf output should be available in stdout, and any other messages or stats in stderr.

    Edit: The documentation does seem a bit wonky, but there is quite a bit of it. You might have better luck with the html in one page or pdf versions, or the man page.

    Also, be sure to pass a string, or similar, to the stdin of the htmldoc process. Passing a StringIO object directly, as was implied by my previous code snippet, won’t work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've built a Django web application and some Django-Piston services. Using a web interface
I am using the built in comment system with Django but it has started
We have a web based application in production built using django. It is deployed
I have an application that makes use of Django's UserProfile to extend the built-in
I maintain a Django webapp for a client of mine. We built it out
I have an application built with Django. Part of it relies on data that
Django has a built in serialization functionality which allows you to serialize any query
I am writing a Django application and using the built-in ORM. I would like
We have an application running built on python/django. I get emails whenever there is
Some background: I am planning to build an application with Django as the backend

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.