【问题标题】:How can I create a DjRF API endpoint that returns a RML-generated PDF?如何创建返回 RML 生成的 PDF 的 DjRF API 端点?
【发布时间】:2019-11-14 11:12:17
【问题描述】:

Django 文档中有以下 ReportLab 示例。

from reportlab.pdfgen import canvas
from django.http import HttpResponse

def some_view(request):
    # Create the HttpResponse object with the appropriate PDF headers.
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'

    # Create the PDF object, using the response object as its "file."
    p = canvas.Canvas(response)

    # Draw things on the PDF. Here's where the PDF generation happens.
    # See the ReportLab documentation for the full list of functionality.
    p.drawString(100, 100, "Hello world.")

    # Close the PDF object cleanly, and we're done.
    p.showPage()
    p.save()
    return response

但是。我想使用 RML 来生成我的 PDF。 ReportLab Plus 提供了一个rml2pdf 函数,可以使用与 Django 模板类似的标记将 RML 文档转换为 PDF。如何向 Django 提供 RML 模板并让 Django 在 API 响应中返回 PDF,类似于文档中的示例?

【问题讨论】:

    标签: django-rest-framework reportlab rml


    【解决方案1】:

    想通了。

    RML 模板

    把它放在你的 Django 模板文件夹中,这样 Django 就可以找到它。

    叫它templates/rml_template.rml

    <!DOCTYPE document SYSTEM "rml.dtd">
    <document filename="example_01.pdf">
     <template>
     <!--this section contains elements of the document -->
     <!--which are FIXED into position. -->
     <pageTemplate id="main">
     <frame id="first" x1="100" y1="400" width="150" height="200"/>
     </pageTemplate>
     </template>
     <stylesheet>
     <!--this section contains the STYLE information for -->
     <!--the document, but there isn't any yet. The tags still -->
     <!--have to be present, however, or the document won't compile.-->
     </stylesheet>
     <story>
     <!--this section contains the FLOWABLE elements of the -->
     <!--document. These elements will fill up the frames -->
     <!--defined in the <template> section above. -->
     <para>
     Welcome to RML!
     </para>
     <para>
     This is the "story". This is the part of the RML document where
     your text is placed.
     </para>
     <para>
     It should be enclosed in "para" and "/para" tags to turn it into
     paragraphs.
     </para>
     </story>
    </document>
    

    Django 视图

    此视图将模板作为字符串加载,rml2pdf 使用响应对象作为要写入的“文件”,类似于 Django 文档示例。

    from django.template import loader
    from rlextra.rml2pdf import rml2pdf
    from django.http import HttpResponse
    
    
    def some_view(request):
        # Create the HttpResponse object with the appropriate PDF headers.
        response = HttpResponse(content_type='application/pdf')
        response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
    
        context = {} # You can add values to be inserted into the template here
    
        rml = str(loader.render_to_string('rml_template.rml', context))
        rml2pdf.go(rml, outputFileName=response)
    
        return response
    

    【讨论】:

      猜你喜欢
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-09
      相关资源
      最近更新 更多