Question

I'm trying to create a PDF with checkboxes that can be checked (using python). I've been trying to use pisa to generate the pdf and have looked around the internet and tried different examples but I cannot find out how to make an editable PDF.

This is my most recent attempt:

import cStringIO
import ho.pisa as pisa
import os

# shortcut for dumping all logs on screen
pisa.showLogging()

def HTML2PDF(data, filename, open=False):
    """
        Simple test showing how to create a PDF file from
        PML Source String. Also shows errors and tries to start
        the resulting PDF
    """
    pdf = pisa.CreatePDF(cStringIO.StringIO(data), file(filename, "wb"))

    if open and not(pdf.err):
        os.startfile(str(filename))

    return not pdf.err

if __name__=="__main__":
    HTMLTEST = """
        <html>
            <body>
                <form name="deleteForm" method="get" action="">
                     User 1 <input type="checkbox" name="user" value="delete" />
                </form>
            </body>
        </html>
   """

   HTML2PDF(HTMLTEST, "test.pdf", open=True)

The form gives me an error:

Traceback (most recent call last):
  File "C:/Users/horeth/PycharmProjects/Reportlab/HTMLtoPF/Main.py", line 32, in 
      HTML2PDF(HTMLTEST, "test.pdf", open=True)
  File "C:/Users/horeth/PycharmProjects/Reportlab/HTMLtoPF/Main.py", line 14, in HTML2PDF
      pdf = pisa.CreatePDF(cStringIO.StringIO(data), file(filename, "wb"))
IOError: [Errno 13] Permission denied: 'test.pdf'

The check boxes are for readers to decide if a user needs to be deleted or not.

I'm wondering if there is a way to create an editable PDF document with Python. This is just one of the attempts I've made so far, as an example.

Was it helpful?

Solution

Possible reasons. You don't have write permission to the directory. The file already exists but you do not have write access to it.

OTHER TIPS

import cStringIO as StringIO
from xhtml2pdf import pisa
from django.template.loader import get_template
from django.template import Context
from cgi import escape

def render_to_pdf(template_path, context_dict): 
    template = get_template(template_path)
    html  = template.render(context_dict)
    result = StringIO.StringIO()
    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-
1")), dest=result)
    if not pdf.err: 
        return HttpResponse(result.getvalue(), 
content_type='application/pdf')
    return HttpResponse('We had some errors<pre>%s</pre>' % 
escape(html))

Call this function

def myview(request):
    return render_to_pdf('HTMLTEST.html', { 'pagesize':'A4',})

create a seperate html file

HTMLTEST.html

    <html>
        <body>
            <form name="deleteForm" method="get" action="">
                 User 1 <input type="checkbox" name="user" value="delete" />
            </form>
        </body>
    </html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top