Python - trml2pdf generating a blank PDF

两盒软妹~` 提交于 2019-12-11 05:18:14

问题


I use trml2pdf library in Python, but even when I use the examples, I get a blank PDF file. I run it as follows: trml2pdf.py ex5.rml > out.pdf

When I open the file in Acrobat it is blank/empty. But when I analyse the contents in text editor, I see the following.

Generated 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 >>

Example 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 >>

What am I doing wrong? Why am I getting blank lines in the output?

Thanks!

Here's basic RML that also returns blank 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>

回答1:


I've been using z3c.rml in many web apps for last 6-8 months and never faced any major issue. rml2pdf command from this package was able to generate the PDF for the rml you have shared.

You should give it a try.

#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:


I am using trml2pdf and it is successfully working , here i am posting my code so that you can take a look

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 Code

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>


来源:https://stackoverflow.com/questions/15141871/python-trml2pdf-generating-a-blank-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!