2013-02-28 72 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>