1

我在 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>
4

2 回答 2

3

在过去的 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
于 2014-04-07T08:04:13.187 回答
3

我正在使用 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 代码

发票.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>
于 2015-12-08T18:01:35.950 回答