Search code examples
pythonpdfpdf-generationpisa

Trying to create an editable pdf with Python


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.


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.