【问题标题】:Python - trml2pdf generating a blank PDFPython - trml2pdf 生成空白 PDF
【发布时间】:2015-12-09 02:01:35
【问题描述】:

我在 Python 中使用 trml2pdf 库,但即使使用示例,我也会得到一个空白 PDF 文件。 我按如下方式运行它: trml2pdf.py ex5.rml > out.pdf

当我在 Acrobat 中打开文件时,它是空白的。 但是当我在文本编辑器中分析内容时,我看到了以下内容。

生成的 PDF:

%PDF-1.4

%“Œ‹ž ReportLab Generated PDF document http://www.reportlab.com

% 'BasicFonts': class PDFDictionary 

1 0 obj

% The standard fonts dictionary

<< /F1 2 0 R

 /F2 3 0 R

 /F3 4 0 R >>

示例 PDF:

%PDF-1.3
%“Œ‹ž ReportLab Generated PDF document http://www.reportlab.com
% 'BasicFonts': class PDFDictionary 
1 0 obj
% The standard fonts dictionary
<< /F1 2 0 R
 /F2 3 0 R
 /F3 4 0 R
 /F4 5 0 R
 /F5 6 0 R >>

我做错了什么?为什么我的输出中有空行?

谢谢!

这里是同样返回空白 PDF 的基本 RML:

<!DOCTYPE document SYSTEM "rml_1_0.dtd">
<document filename="example_1.pdf">
<stylesheet>
</stylesheet>
<pageDrawing>
     <drawCentredString x="4.1in" y="5.8in">
       Hello World.
 </drawCentredString>
</pageDrawing>
</document>

【问题讨论】:

  • 也许你应该给我们看一下 rml 文件。您是否尝试过使用最小的 rml?
  • 是的,我确实尝试过使用最小的 rml。我也尝试过不同的。话虽如此,如果解析器不满意 - 它会失败并显示一条消息(无效的标签值)。所以它似乎很高兴,它确实产生了一个文件。只是它包含了那些奇怪的换行符..
  • 如果您不给我们一些 rml 最小代码来查看问题,则很难提供帮助。
  • So OK

标签: python pdf rml


【解决方案1】:

在过去的 6-8 个月里,我一直在许多网络应用程序中使用 z3c.rml,并且从未遇到任何重大问题。此包中的rml2pdf 命令能够为您共享的 rml 生成 PDF。

你应该试一试。

#Install z3c.rml
[sudo] pip install z3c.rml

# create a new rml document
vim example.rml

# rum rml2pdf command to convert this rml to pdf
rml2pdf example.rml

# You should have desired PDF file now

【讨论】:

【解决方案2】:

我正在使用 trml2pdf,它运行成功,我在这里发布我的代码,以便您查看

from django.template.loader import get_template
from django.template.context import Context
import trml2pdf
def invoicepdf(request):
    template = get_template('invoice.rml')
    context = Context({
        'name': "XXXXXXXXXX", 
        'date':_date, 
        'subtotal':1909201,
        'total': 345789

     })
    xmlstring = template.render(context)
    pdfstr = trml2pdf.parseString(xmlstring)
    response = HttpResponse(mimetype='application/pdf')
    response.write(pdfstr)
    response['Content-Disposition'] = 'attachment; filename=invoice.pdf'
    return response

RML 代码

invoice.rml

<!-- This is very Invoice RML template for illustrative purposes.  -->
<!-- A basic RML template has three sections.  The 'template'     --> 
<!-- section is where you define fixed position elements, along   -->
<!-- with 'frames' containing  flowing text.  The 'stylesheet'    -->
<!-- section contains re-useable text style definitions.  The     -->
<!-- 'story' section contains the text and graphics which fill    -->
<!-- up the frames defined in the template section.               -->
<!-- For more information, please read the documentation at       -->
<!-- http://www.reportlab.com/software/documentation/             -->

<!DOCTYPE document SYSTEM "rml.dtd">
<document filename="invoice.pdf">
    <!-- left margin -->
<template title="Invoices" author="Atul jain" allowSplitting="20">
<pageTemplate id="first">
  <frame id="first" x1="34.0" y1="28.0" width="530" height="786"/>
  <pageGraphics>
    <lines>0.3cm 27.0cm 20cm 27.0cm</lines>
  </pageGraphics>
</pageTemplate>
 </template> 
 <story>
<para>
  <font color="white"> </font>
</para>
<para><b>Name:- {{name}} </b></para>
<para><b>Date:- {{date}} </b></para>
  <blockTable colWidths="385.0,60.0,85.0">
  <tr>
    <td>
      <para>
        <font color="white"> </font>
      </para>
    </td>
    <td>
      <para>Net Total:</para>
    </td>
    <td>
      <para>{{subtotal}} INR;</para>
    </td>
  </tr>
  <tr>
    <td>
      <para>
        <font color="white"> </font>
      </para>
    </td>
    <td>
      <para><b>Total:</b></para>
    </td>
    <td>
      <para><b>{{total}} INR;</b></para>
    </td>
  </tr>
</blockTable>   
 </story>
</document>

【讨论】:

    猜你喜欢
    • 2012-09-13
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 2012-01-04
    相关资源
    最近更新 更多