"Fossies" - the Fresh Open Source Software Archive

Member "xhtml2pdf-0.2.8/xhtml2pdf/builders/watermarks.py" (16 Jun 2022, 4390 Bytes) of package /linux/www/xhtml2pdf-0.2.8.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 "watermarks.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.2.7_vs_0.2.8.

    1 import PyPDF3
    2 from PIL import Image
    3 from reportlab.pdfgen.canvas import Canvas
    4 
    5 from xhtml2pdf.files import pisaFileObject, getFile
    6 
    7 
    8 class WaterMarks:
    9     @staticmethod
   10     def get_size_location(img, context, pagesize, is_portrait):
   11         object_position = context.get('object_position', None)
   12         cssheight = context.get('height', None)
   13         csswidth = context.get('width', None)
   14         iw, ih = img.getSize()
   15         pw, ph = pagesize
   16         width = pw  # min(iw, pw) # max
   17         wfactor = float(width) / iw
   18         height = ph  # min(ih, ph) # max
   19         hfactor = float(height) / ih
   20         factor_min = min(wfactor, hfactor)
   21         factor_max = max(wfactor, hfactor)
   22         if is_portrait:
   23             height = ih * factor_min
   24             width = iw * factor_min
   25         else:
   26             height = ih * factor_max
   27             width = iw * factor_min
   28 
   29         if object_position:
   30             # x, y, width=None, height=None
   31             x, y = object_position
   32         else:
   33             if is_portrait:
   34 
   35                 x, y = 0, ph-height
   36             else:
   37                 x, y = 0, 0
   38         if csswidth:
   39             width=csswidth
   40         if cssheight:
   41             height=cssheight
   42 
   43         return x, y, width, height
   44 
   45     @staticmethod
   46     def get_img_with_opacity(pisafile, context):
   47         opacity = context.get('opacity', None)
   48         if opacity:
   49             name = pisafile.getNamedFile()
   50             img=Image.open(name)
   51             img = img.convert('RGBA')
   52             img.putalpha(int(255*opacity))
   53             img.save(name,"PNG")
   54             return getFile(name).getBytesIO()
   55         return pisafile.getBytesIO()
   56 
   57     @staticmethod
   58     def generate_pdf_background(pisafile, pagesize, is_portrait, context={}):
   59         """
   60         PyPDF3 requires pdf as background so convert image to pdf in temporary file with same page dimensions
   61         :param pisafile:  Image File
   62         :param pagesize:  Page size for the new pdf
   63         :return: pisaFileObject as tempfile
   64         """
   65         # don't move up, we are preventing circular import
   66         from xhtml2pdf.xhtml2pdf_reportlab import PmlImageReader
   67         output = pisaFileObject(None, "application/pdf") # build temporary file
   68         img = PmlImageReader(
   69             WaterMarks.get_img_with_opacity(pisafile, context)
   70         )
   71         x, y, width, height = WaterMarks.get_size_location(img, context, pagesize, is_portrait)
   72 
   73         canvas = Canvas(output.getNamedFile(), pagesize=pagesize)
   74         canvas.drawImage(img, x, y, width, height, mask='auto')
   75 
   76         """
   77         iw, ih = img.getSize()
   78         pw, ph = pagesize
   79 
   80         width = pw  # min(iw, pw) # max
   81         wfactor = float(width) / iw
   82         height = ph  # min(ih, ph) # max
   83         hfactor = float(height) / ih
   84         factor_min = min(wfactor, hfactor)
   85         factor_max = max(wfactor, hfactor)
   86         
   87         if is_portrait:
   88             w = iw * factor_min
   89             h = ih * factor_min
   90             canvas.drawImage(img, 0, ph - h, w, h)
   91         else:
   92             h = ih * factor_max
   93             w = iw * factor_min
   94             canvas.drawImage(img, 0, 0, w, h)
   95         """
   96         canvas.save()
   97 
   98         return output
   99 
  100     @staticmethod
  101     def get_watermark(context, max_numpage):
  102         if context.pisaBackgroundList:
  103             pages = list(map(lambda x: x[0], context.pisaBackgroundList))+[max_numpage+1]
  104             pages.pop(0)
  105             counter=0
  106             for page, bgfile, pgcontext in context.pisaBackgroundList:
  107                 if not bgfile.notFound():
  108                     yield range(page, pages[counter]), bgfile, int(pgcontext['step'])
  109                 counter+=1
  110 
  111     @staticmethod
  112     def process_doc(context, istream, output):
  113         pdfoutput = PyPDF3.PdfFileWriter()
  114         input1 = PyPDF3.PdfFileReader(istream)
  115         has_bg=False
  116         for pages, bgouter, step in WaterMarks.get_watermark(context, input1.numPages):
  117             for index, ctr in enumerate(pages):
  118                 bginput = PyPDF3.PdfFileReader(bgouter.getBytesIO())
  119                 pagebg = bginput.getPage(0)
  120                 page = input1.getPage(ctr-1)
  121                 if index%step == 0:
  122                     pagebg.mergePage(page)
  123                     page = pagebg
  124                 pdfoutput.addPage(page)
  125                 has_bg=True
  126         if has_bg:
  127             pdfoutput.write(output)
  128 
  129         return output, has_bg