我正在尝试使用可以检查的复选框创建PDF(使用python)。我一直在尝试使用比萨生成pdf,并浏览了互联网并尝试了不同的例子,但我无法找到如何制作可编辑的PDF。
这是我最近的尝试:
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)
表格给我一个错误:
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'
复选框供读者决定是否需要删除用户。
我想知道是否有办法用Python创建可编辑的PDF文档。这只是我迄今为止所做的一次尝试,作为一个例子。
答案 0 :(得分:1)
可能的原因。您没有该目录的写许可权。该文件已存在,但您没有写入权限。
答案 1 :(得分: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',})
HTMLTEST.html
<html>
<body>
<form name="deleteForm" method="get" action="">
User 1 <input type="checkbox" name="user" value="delete" />
</form>
</body>
</html>