2013-02-28 169 views
0

我在Python中使用trml2pdf库,但即使在使用示例时,我也会得到一个空白PDF文件。 我运行它如下: trml2pdf.py ex5.rml> out.pdfPython - trml2pdf生成一个空白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 >> 

我在做什么错?为什么我在输出中出现空行?

谢谢!

这里的基本RML也返回空白的PDF:

<!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> 
+0

也许你应该向我们展示rml文件。你有没有尝试过最小的rml? – 2013-02-28 17:32:17

+0

是的,我确实尝试了最小的rml。我也尝试了不同的。话虽如此,如果解析器不满意 - 它会失败并显示一条消息(无效标记值)。所以它似乎很高兴,它确实会产生一个文件。只是它包含那些奇怪的换行符。 – 2013-02-28 18:02:28

+0

如果您不给我们一些rml最小代码来查看问题,则很难提供帮助。 – 2013-03-01 08:04:30

回答

2

我一直在使用z3c.rml在许多Web应用程序的最后6-8个月,从来没有遇到任何重大问题。来自此软件包的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 
+0

所以,也许你现在回答这个问题? http://stackoverflow.com/questions/25821336/wrong-breaking-columns-in-reportlab-rml – 2014-09-13 08:31:31

+1

太棒了!您的答案解决了https://stackoverflow.com/questions/33951499/how-to-remove-the-default-footers-when-using-rml2pdf的问题 – 2017-11-13 01:50:32

1

我使用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>