python - 使用格式为 PDF/X-1a 的 Python 将 HTML 转换为 PDF

标签 python django pdf xhtml2pdf

我有一个网站可以使用 Django 中名为“XHTML2PDF”的库将 HTML 转换为 PDF。

问题是我需要使用 PDF/X-1a 格式转换此 PDF。我不知道是否可以更改 XHTML2PDF 中的格式,如果不能,有人知道替代方案吗?

查看:

 def book(request):
        # Prepare context
        data = {}
        data['today'] = datetime.date.today()
        data['farmer'] = 'Old MacDonald'
        data['animals'] = [('Cow', 'Moo'), ('Goat', 'Baa'), ('Pig', 'Oink')]

        # Render html content through html template with context
        template = get_template('book_number_one.html')
        html  = template.render(Context(data))

        # Write PDF to file
        file = open('test.pdf', "w+b")
        pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file,
                encoding='utf-8')

        # Return PDF document through a Django HTTP response
        file.seek(0)
        pdf = file.read()
        file.close()            # Don't forget to close the file handle
        return HttpResponse(pdf, mimetype='application/pdf')

模板:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Background</title>
        <style>
            @page p1 {
                background-image: url("nu/COVER-FRONT-ES.png");
                size: 210mm 210mm;
                @frame text {
                    top: 6cm;
                    left: 4cm;
                    right: 4cm;
                    bottom: 4cm;
                }
            }
            @page p2 {
                background-image: url("nu/001.png");
                size: 210mm 210mm;
                @frame text {
                    top: 6cm;
                    left: 4cm;
                    right: 4cm;
                    bottom: 4cm;
                    border:0;
                }
            }
            @font-face {
                font-family: 'Gilles';
                src: url(/gilles/gilles.ttf);
                font-weight: normal;
                font-style: normal;
            }
        </style>
    </head>
    <body>
        <pdf:nextpage name="p1" />

        <pdf:nextpage name="p2" />

        <p class="page-2">
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. <br />
            Vitae, natus, possimus, placeat consequuntur iste at sapiente perferendis <br />
            delectus suscipit dolorem dignissimos quaerat quia ex fuga officia dolore <br />
            asperiores sint mollitia.<br />
            <br />
        </p>
    </body>
</html>

最佳答案

我建议使用名为“django-easy-pdf”的库

https://pypi.python.org/pypi/django-easy-pdf

演示:here

{% extends "easy_pdf/base.html" %}

{% block content %}
    <div id="content">
        <h1>Hi there!</h1>
    </div>
{% endblock %}


from easy_pdf.views import PDFTemplateView

class HelloPDFView(PDFTemplateView):
    template_name = "hello.html"

关于python - 使用格式为 PDF/X-1a 的 Python 将 HTML 转换为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23960769/

相关文章:

Python 到 ruby​​ 的转换

python - 没有声明编码

pdf - Pandoc:创建表格,Markdown 到 PDF

python - 除了 Javascript shell 之外,MongoDB 是否还有 Ruby Shell 或 Python Shell?

c++ - 我应该在 C++ 之后学习 Python 吗?

python - Django - importError 在/博客。 (没有名为 urls 的模块)

Django Rest Framework 在网站内重用 API 逻辑

php - 在 PHP 中将文本转换为 PDF

pdf - Grails 渲染插件在部署时不渲染图像

python - findAll-beautifulsoup-python 无法正常工作