0

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.

4

2 回答 2

1

可能的原因。您没有对该目录的写入权限。该文件已存在,但您没有对其的写入权限。

于 2013-10-10T20:47:22.047 回答
0
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))

调用这个函数

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

创建一个单独的html文件

HTMLTEST.html

    <html>
        <body>
            <form name="deleteForm" method="get" action="">
                 User 1 <input type="checkbox" name="user" value="delete" />
            </form>
        </body>
    </html>
于 2017-07-25T05:51:40.883 回答