我在将一个页脚显示为比萨文档的第一页上的一个框架时,以及在每个其他页面上显示为另一个框架时遇到了一些麻烦.我试图从这里调整lastPage的想法,但没有运气.
是否有可能做到这一点?<pdf:nextpage />这似乎不是正确的事情,因为文档有一个可能(或可能不)流过多个页面的长表.<pdf:nextframe />加上只有第一页的框架看起来很有希望,但我不确定如何使用它.
目前我(为了简洁起见):
<style type="text/css">
@page {
margin: 1cm;
margin-bottom: 2.5cm;
@frame footer {
-pdf-frame-content: footerFirst;
-pdf-frame-border: 1;
bottom: 2cm;
margin-left: 1cm;
margin-right: 1cm;
height: 1cm;
}
@frame footer {
-pdf-frame-content: footerOther;
bottom: 2cm;
margin-left: 1cm;
margin-right: 1cm;
height: 1cm;
}
</style>
Run Code Online (Sandbox Code Playgroud)
<body>
<table repeat="1">
<!-- extra long table here -->
</table>
<div id="footerContent">This is a footer</div>
<!-- what goes here to switch frames after the first page? -->
<div id="footerOther"></div>
</body> …Run Code Online (Sandbox Code Playgroud) 我在html页面中使用了unicode,该页面在html页面中正确显示。但是,当使用xhtml2pdf将其转换为html时,它会在unicode中生成黑色的实心方形框。除UTF-8设置外,是否还有一些Unicode设置。我不认为它的unicode问题。
# convert HTML to PDF
pisaStatus = pisa.CreatePDF(
StringIO(sourceHtml.encode('utf-8')),
dest=resultFile)
Run Code Online (Sandbox Code Playgroud)
# -*- coding: utf-8 -*-
from xhtml2pdf import pisa
from StringIO import StringIO
source = """<html>
<style>
@font-face {
font-family: Preeti;
src: url("preeti.ttf");
}
body {
font-family: Preeti;
}
</style>
<body>
This is a test <br/>
???
</body>
</html>"""
# Utility function
def convertHtmlToPdf(source):
# open output file for writing (truncated binary)
pdf = StringIO()
pisaStatus = pisa.CreatePDF(StringIO(source.encode('utf-8')), pdf)
# return True on success and False on errors …Run Code Online (Sandbox Code Playgroud) 当我同时使用 django-admin-tools(0.8.0) 和 django-tables2(1.5) 时,尝试通过 Django(1.10.4) 中的 xhtml2pdf 渲染 PDF 时遇到问题。我已经做了足够的阅读来了解正在发生的事情的基础,但不知道如何解决它。我认为这与 django-admin-tools 自定义加载器有关。
链接到我从 django-tables 获得的异常。
这个问题让我提出了一个问题。
我想做的要点是通过 django-admin 界面中 AdminModel 的下拉框创建一个自定义管理“操作”,将给定的查询集转换为 PDF 文档。
根据 django-tables2文档, render() 函数需要 3 个参数(request, 'template_name.html', {'people': Person.objects.all()})。因此,我向 context_dict 添加了一个查询集,并尝试在下面模板的 for 循环中使用它,但没有骰子。该模板在没有 django-tables2 的情况下将 html 渲染为 pdf 就很好,但是如果我尝试将表格转换为 PDF,我会得到以下内容...
Environment:
Request Method: POST
Request URL: http://127.0.0.1:9999/admin/research/labsample/
Django Version: 1.10.4
Python Version: 3.5.2
Installed Applications:
('admin_tools',
'admin_tools.theming',
'admin_tools.menu',
'admin_tools.dashboard',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_tables2',
'import_export',
'chemicals',
'suppliers',
'customers', …Run Code Online (Sandbox Code Playgroud) 我正在构建一个项目,我需要将数据从模板(html)转换为pdf,我的问题是图像没有出现在pdf视图上。这是我的代码:
class GeneratePdf_month(TemplateView):
template_name = "polls/info_month.html"
def get(self, request, *args, **kwargs):
## do some....
data = {
'cliente': cliente,
}
pdf = render_to_pdf(self.template_name, data)
return HttpResponse(pdf, content_type='application/pdf')
## and this is my html template
<head>
{% load staticfiles %}
<title>Detail</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 10px;
}
</style>
</head>
<body>
<header>BlaBla<img src="{% static 'polls/images/image.png'%}"></header>
</body
Run Code Online (Sandbox Code Playgroud)
有人能帮我吗?
我知道基于Python中的pdf创建有很多问题,但我没有看到任何基于使用Pisa或xhtml2pdf创建pdf的内容.
这是我的代码.
pisa.pisaDocument(cStringIO.StringIO(a).encode('utf-8'),file('mypdf.pdf','wb'))
Run Code Online (Sandbox Code Playgroud)
然后
pisa.startViewer('mypdf.pdf')
Run Code Online (Sandbox Code Playgroud)
我在几个不同的教程和示例中汇编了这个,但是我尝试的每一件事总是导致pdf被破坏,并且在尝试打开pdf时我得到了这个消息.
"Adobe Reader无法打开'awesomer.pdf',因为它不是受支持的文件类型,或者因为文件已损坏(例如,它是作为电子邮件附件发送的,并且未正确解码)."
即使我不在字符串上使用.encode('utf-8'),也会出现此消息.
我究竟做错了什么?我的Mac上的编码是否与此有关?
我想尝试django-easy-pdf一下,但是我在安装依赖项时遇到了问题.
我ImportError: No module named xhtml2pdf.default在运行Django时得到了.
为了尝试修复,我做了一个pip install xhtml2pdf,但是产生了Could not find any downloads that satisfy the requirement pyPdf (from xhtml2pdf).
所以,让我们pip install --allow-unverified pyPdf pyPdf来解决这个问题.这完成没有问题.重复时pip install xhtml2pdf,软件安装没有问题.
但是,当启动Django 1.6时,我得到:ImportError: Reportlab Version 2.1+ is needed!.
我确实安装了reportlab 2.2或更高版本,因为pip freeze列表3.0.
它看起来像是在几个文件(util.py和__init__.py)中硬编码:
if not (reportlab.Version[0] == "2" and reportlab.Version[2] >= "1"):
raise ImportError("Reportlab Version 2.1+ is needed!")
if not REPORTLAB22:
raise ImportError, "Reportlab Toolkit …Run Code Online (Sandbox Code Playgroud) 我很难在Django中制作pdf文件.根据我之前的问题,我遇到了同样的错误.
当我运行以下代码时'list' object has no attribute 'encode',pdf保存在媒体文件夹中,但它是纯文本文件.我认为被引用的对象是查询集sections.
@login_required
def generate_pdf(request, slug):
# Prepare context
document = get_object_or_404(Document, slug=slug)
sections = \
get_list_or_404(Section.objects.filter
(associated_document__startswith=document.slug))
data = {'document': document, 'sections': sections}
# Render html content through html template with context
template = get_template('lld/lld_pdf.html')
html = template.render(Context(data))
file = open('/home/project/media/test.pdf', "w+b")
print type(document)
print type(sections)
print type(data)
print type(template)
print type(html)
print type(file)
pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file)
# Return PDF document through a Django HTTP …Run Code Online (Sandbox Code Playgroud) 我想,以改善创造了我生成的PDFxhtml2pdf有一些CSS style在我的Django Project。
我想例如justify a text block,但显然,它不起作用,我不知道为什么。我正在阅读很多关于这个库的文档,我写了一些 CSS 脚本,但仍然没有。
这是我的 HTML 文件:
<html>
<head>
{% load staticfiles %}
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" />
<style>
p {text-align: justify;}
</style>
</head>
<font face="Courier New, monospace"
<body>
<logo class="logo">
<img src='/Users/valentinjungbluth/Desktop/Django/Etat_civil/Etat_civil/static/images/logo.png' />
</logo>
<h2 align="center"> <font color="red" size="6"> ACTE DE NAISSANCE <br /> COPIE INTEGRALE </font></align> </h2>
<br></br>
<br></br>
{% block content %}
<h3 align="left"> ACTE DE NAISSANCE N° {{birthcertificate.id}} / …Run Code Online (Sandbox Code Playgroud) 转换:
html = template.render(context)
resultFile = open(filepath, "w+b")
pdf = pisa.CreatePDF(html.encode('utf-8'), dest=resultFile,encoding='utf-8', link_callback=link_callback)
Run Code Online (Sandbox Code Playgroud)
链接回调
def link_callback(uri, rel):
sUrl = settings.STATIC_URL # Typically /static/
sRoot = settings.STATIC_ROOT
mUrl = settings.MEDIA_URL # Typically /static/media/
mRoot = settings.MEDIA_ROOT # Typically /home/userX/project_static/media/
# convert URIs to absolute system paths
if uri.startswith(mUrl):
path = os.path.join(mRoot, uri.replace(mUrl, ""))
elif uri.startswith(sUrl):
path = os.path.join(sRoot, uri.replace(sUrl, ""))
else:
return uri # handle absolute uri (ie: http://some.tld/foo.png)
# make sure that file exists
if not os.path.isfile(path):
raise Exception(
'media …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 xhtml2pdf 应用程序保存生成的 PDF。我希望它将 pdf 保存到模型中。pdf 已完美创建,浏览器会下载该文件,但我不想下载,而是将其保存到模型中,在我的 models.py 中,我告诉它保存到媒体文件夹中的文件夹中。
模型.py:
class purchase(models.Model):
purchase_id = models.AutoField(primary_key=True)
transaction_date = models.DateTimeField()
method_payment = models.CharField(max_length = 20)
cmr_name = models.CharField(max_length = 60)
cmr_address = models.CharField(max_length = 90)
cmr_postcode = models.CharField(max_length = 10)
cmr_tel = models.CharField(max_length = 14)
cmr_email = models.CharField(max_length = 40)
cmr_pod = models.FileField(upload_to='customers_id')
cmr_signature = JSignatureField()
receipt = models.FileField(upload_to='receipts', null=True, blank=True)
added_by = models.CharField(max_length = 30)
item_brand = models.CharField(max_length = 50)
product = models.CharField(max_length = 100)
model = models.CharField(max_length = 100)
serial …Run Code Online (Sandbox Code Playgroud) 我在理解如何安装xhtml2pdf时遇到了问题.我按照Chris Glass网站上的说明进行了虚拟环境设置并运行了单元测试.当我在我的views.py文件顶部插入以下内容时:
import ho.pisa as pisa
import cStringIO as StringIO
import cgi
import os
Run Code Online (Sandbox Code Playgroud)
我得到一个错误说No module named ho.pisa (我预期).我如何获得django甚至python命令行导入此模块(因为我尝试在命令行中导入模块具有相同的成功级别).
编辑
为什么我需要虚拟环境.我不能只是xhtml2pdf作为一个包安装并正常导入或者我会破坏一些Django/python的东西吗?
编辑
我还从xhtml2pdf虚拟环境中运行了django,但(xhtml2pdfenv)person@person-laptop:~/Software/django$ ./manage.py runserver仍拒绝导入xhtm2pdf
我使用python xhtml2pdf模块和pisa类来创建PDF.现在PDF是垂直A4格式,如何更改它以使PDF格式化为横向布局?
import xhtml2pdf.pisa as pisa
result = StringIO()
pdf = pisa.CreatePDF(
StringIO("This is my PDF"),
result)
Run Code Online (Sandbox Code Playgroud) 我不知道为什么会发生这种情况。每当我添加阿拉伯语内容时,它都会显示上述错误,否则在英语语言中工作正常\n相同的代码在另一个项目中可以完美工作,但在这里不行。\n视图.py
\ndef generate_pdf_for_gift_and_add_to_cart(request):\n cart = Cart(request)\n if request.method == 'POST':\n id = request.POST.get('project_id')\n selectedAmount = request.POST.get('amount')\n senderNameDonatedDonationPage = request.POST.get(\n 'senderNameDonatedDonationPage')\n receiverNameDonatedDonationPage = request.POST.get(\n 'receiverNameDonatedDonationPage')\n phoneNumberDonatedDonationPage = request.POST.get(\n 'phoneNumberDonatedDonationPage')\n emailDonatedDonationPage = request.POST.get('emailDonatedDonationPage')\n params = {\n "project_id": id,\n "selectedAmount": selectedAmount,\n }\n\n pdf = render_to_pdf('pdfs/gift_pdf.html', params)\n if pdf:\n response = HttpResponse(pdf, content_type='application/pdf')\n filename = f"Invoice_{emailDonatedDonationPage}_{datetime.now()}.pdf"\n content = "inline; filename='%s'" % filename\n download = request.POST.get("download")\n if download:\n content = "filename='%s'" % filename\n response['Content-Disposition'] = content\n receipt_file = BytesIO(pdf.content)\n return …Run Code Online (Sandbox Code Playgroud)