• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / z3c.rml / 16098868126

14 Apr 2025 06:50AM UTC coverage: 87.385%. Remained the same
16098868126

push

github

icemac
Back to development: 5.1

561 of 792 branches covered (70.83%)

Branch coverage included in aggregate %.

3990 of 4416 relevant lines covered (90.35%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.43
/src/z3c/rml/rml2pdf.py
1
##############################################################################
2
#
3
# Copyright (c) 2007 Zope Foundation and Contributors.
4
# All Rights Reserved.
5
#
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
12
#
13
##############################################################################
14
"""RML to PDF Converter
15
"""
16
import argparse
1✔
17
import io
1✔
18
import os
1✔
19
import sys
1✔
20

21
import zope.interface
1✔
22
from lxml import etree
1✔
23

24
from z3c.rml import document
1✔
25
from z3c.rml import interfaces
1✔
26

27

28
zope.interface.moduleProvides(interfaces.IRML2PDF)
1✔
29

30

31
def parseString(xml, removeEncodingLine=True, filename=None):
1✔
32
    if isinstance(xml, str) and removeEncodingLine:
1!
33
        # RML is a unicode string, but oftentimes documents declare their
34
        # encoding using <?xml ...>. Unfortuantely, I cannot tell lxml to
35
        # ignore that directive. Thus we remove it.
36
        if xml.startswith('<?xml'):
1✔
37
            xml = xml.split('\n', 1)[-1]
1✔
38
    root = etree.fromstring(xml)
1✔
39
    doc = document.Document(root)
1✔
40
    if filename:
1✔
41
        doc.filename = filename
1✔
42
    output = io.BytesIO()
1✔
43
    doc.process(output)
1✔
44
    output.seek(0)
1✔
45
    return output
1✔
46

47

48
def go(xmlInputName, outputFileName=None, outDir=None, dtdDir=None):
1✔
49
    if hasattr(xmlInputName, 'read'):
1✔
50
        # it is already a file-like object
51
        xmlFile = xmlInputName
1✔
52
        xmlInputName = 'input.pdf'
1✔
53
    else:
54
        with open(xmlInputName, 'rb') as xmlFile:
1✔
55
            return go(xmlFile, outputFileName, outDir, dtdDir)
1✔
56

57
    # If an output filename is specified, create an output file for it
58
    outputFile = None
1✔
59
    if outputFileName is not None:
1✔
60
        if hasattr(outputFileName, 'write'):
1✔
61
            # it is already a file-like object
62
            outputFile = outputFileName
1✔
63
            outputFileName = 'output.pdf'
1✔
64
        else:
65
            if outDir is not None:
1!
66
                outputFileName = os.path.join(outDir, outputFileName)
×
67
            with open(outputFileName, 'wb') as outputFile:
1✔
68
                return go(xmlFile, outputFile, outDir, dtdDir)
1✔
69

70
    if dtdDir is not None:
1!
71
        sys.stderr.write('The ``dtdDir`` option is not yet supported.\n')
×
72

73
    root = etree.parse(xmlFile).getroot()
1✔
74
    doc = document.Document(root)
1✔
75
    doc.filename = xmlInputName
1✔
76

77
    # Create a Reportlab canvas by processing the document
78
    doc.process(outputFile)
1✔
79

80

81
def main(args=None):
1✔
82
    if args is None:
1!
83
        parser = argparse.ArgumentParser(
×
84
            prog='rml2pdf',
85
            description='Converts file in RML format into PDF file.',
86
            epilog='Copyright (c) 2007 Zope Foundation and Contributors.'
87
        )
88
        parser.add_argument('xmlInputName', help='RML file to be processed')
×
89
        parser.add_argument(
×
90
            'outputFileName',
91
            nargs='?',
92
            help='output PDF file name')
93
        parser.add_argument('outDir', nargs='?', help='output directory')
×
94
        parser.add_argument(
×
95
            'dtdDir',
96
            nargs='?',
97
            help='directory with XML DTD (not yet supported)')
98
        pargs = parser.parse_args()
×
99
        args = (
×
100
            pargs.xmlInputName,
101
            pargs.outputFileName,
102
            pargs.outDir,
103
            pargs.dtdDir)
104

105
    go(*args)
1✔
106

107

108
if __name__ == '__main__':
109
    canvas = go(sys.argv[1])
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc