"Fossies" - the Fresh Open Source Software Archive

Member "xhtml2pdf-0.2.5/xhtml2pdf/pdf.py" (25 Sep 2020, 1927 Bytes) of package /linux/www/xhtml2pdf-0.2.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "pdf.py" see the Fossies "Dox" file reference documentation.

    1 # -*- coding: utf-8 -*-
    2 
    3 # Copyright 2010 Dirk Holtwick, holtwick.it
    4 #
    5 # Licensed under the Apache License, Version 2.0 (the "License");
    6 # you may not use this file except in compliance with the License.
    7 # You may obtain a copy of the License at
    8 #
    9 #     http://www.apache.org/licenses/LICENSE-2.0
   10 #
   11 # Unless required by applicable law or agreed to in writing, software
   12 # distributed under the License is distributed on an "AS IS" BASIS,
   13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14 # See the License for the specific language governing permissions and
   15 # limitations under the License.
   16 
   17 import six
   18 import logging
   19 
   20 
   21 from xhtml2pdf.util import pisaTempFile, getFile, PyPDF2
   22 
   23 
   24 log = logging.getLogger("xhtml2pdf")
   25 
   26 
   27 class pisaPDF:
   28     def __init__(self, capacity=-1):
   29         self.capacity = capacity
   30         self.files = []
   31 
   32     def addFromURI(self, url, basepath=None):
   33         obj = getFile(url, basepath)
   34         if obj and (not obj.notFound()):
   35             self.files.append(obj.getFile())
   36 
   37     addFromFileName = addFromURI
   38 
   39     def addFromFile(self, f):
   40         if hasattr(f, "read"):
   41             self.files.append(f)
   42         else:
   43             self.addFromURI(f)
   44 
   45     def addFromString(self, data):
   46         self.files.append(pisaTempFile(data, capacity=self.capacity))
   47 
   48     def addDocument(self, doc):
   49         if hasattr(doc.dest, "read"):
   50             self.files.append(doc.dest)
   51 
   52     def join(self, file=None):
   53         output = PyPDF2.PdfFileWriter()
   54         for pdffile in self.files:
   55             input = PyPDF2.PdfFileReader(pdffile)
   56             for pageNumber in six.moves.range(input.getNumPages()):
   57                 output.addPage(input.getPage(pageNumber))
   58 
   59         if file is not None:
   60             output.write(file)
   61             return file
   62         out = pisaTempFile(capacity=self.capacity)
   63         output.write(out)
   64         return out.getvalue()
   65 
   66     getvalue = join
   67     __str__ = join