wkhtmltopdf-django动态模板渲染

发布于 2025-01-26 22:01:28 字数 1245 浏览 2 评论 0 原文

有什么方法可以动态生成PDF,并且内容从数据库中获取?

我的目标是允许用户从Wysiwyg编辑器中生成自己的PDF设计。

这是我的模型

    class DocumentService(models.Model):
        name = models.CharField(max_length=60, blank=True)
        html_content = models.TextField(blank=True)

,我使用django-wkhtmltopdf将HTML渲染为PDF。

    response = PDFTemplateResponse(
            request=request,
            template="document.html",
            filename=filename,
            context=data,
            show_content_in_browser=True,
            cmd_options=cmd_options,
        )

如何将此记录的内容渲染到我的PDF中?

    document = DocumentService.objects.get(pk=1)
    document.html_content # <-- this content

HTML内容应该是这样的

<html>
  <body>
    <p>
      <strong>Date: </strong> {{date}}
      <strong>User: </strong> {{user.name}}
      <strong>Email: </strong> {{user.email}}
      <strong>Info: </strong> {{user.info}}
      ...
    </p>
  </body>
 </html>

Is there any way to generate a PDF dynamically, with content being fetched from the database?

My goal is to allow my users to generate their own PDF design from a wysiwyg editor.

This is my model

    class DocumentService(models.Model):
        name = models.CharField(max_length=60, blank=True)
        html_content = models.TextField(blank=True)

I use django-wkhtmltopdf to render html to PDF.

    response = PDFTemplateResponse(
            request=request,
            template="document.html",
            filename=filename,
            context=data,
            show_content_in_browser=True,
            cmd_options=cmd_options,
        )

How can I render the content of this record into my PDF?

    document = DocumentService.objects.get(pk=1)
    document.html_content # <-- this content

The HTML content should be something like this

<html>
  <body>
    <p>
      <strong>Date: </strong> {{date}}
      <strong>User: </strong> {{user.name}}
      <strong>Email: </strong> {{user.email}}
      <strong>Info: </strong> {{user.info}}
      ...
    </p>
  </body>
 </html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽你眉间 2025-02-02 22:01:28

只需使用

from django.template import engines

document = DocumentService.objects.get(pk=1)
data = {'data': document.html_content}
str_template = render_to_string("document.html", data) 
response = PDFTemplateResponse(
            request=request,
            template= engines['django'].from_string(str_template),
            filename=filename,
            context=data,
            show_content_in_browser=True,
            cmd_options=cmd_options,
        )

以及您的 document.html

<body>
 {{data|safe}} <!--- Use safe if you want to render html --->
</body>

Just use engines.from_string(template_code)

from django.template import engines

document = DocumentService.objects.get(pk=1)
data = {'data': document.html_content}
str_template = render_to_string("document.html", data) 
response = PDFTemplateResponse(
            request=request,
            template= engines['django'].from_string(str_template),
            filename=filename,
            context=data,
            show_content_in_browser=True,
            cmd_options=cmd_options,
        )

and inside your document.html you've to add like this

<body>
 {{data|safe}} <!--- Use safe if you want to render html --->
</body>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文