def xhtml2pdf(html, timeoutseconds=30): ''' Takes custom (pisa style) xhtml and makes an pdf document out of it Calls pisa `xhtml2pdf` from the commandline, takes xhtml with embedded css and returns pdf Raises IOError (descriptive text, returncode, stderr) in case something went wrong ''' if isinstance(html, unicode): html = html.encode("utf-8") cmd = [which('xhtml2pdf').next(), '-q', '-', '-'] with tempfile.NamedTemporaryFile() as t: t.write(html); t.flush(); t.seek(0) popen = ecs.utils.killableprocess.Popen(cmd, stdin=t, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = popen.communicate() if popen.returncode != 0: raise IOError('xhtml2pdf pipeline returned with errorcode %i , stderr: %s' % (popen.returncode, stderr)) return stdout
def pdf_barcodestamp(source_filelike, dest_filelike, barcode1, barcode2=None, timeoutseconds=30): ''' takes source pdf, stamps a barcode into it and output it to dest raises IOError if something goes wrong (including exit errorcode and stderr output attached) ''' if barcode2: raise(NotImplementedError) # TODO: barcode2_content is currently unimplemented # render barcode template to ready to use postscript file template = loader.get_template('xhtml2pdf/barcode.ps') barcode_ps = template.render(Context({'barcode': barcode1})) try: # render barcode postscript file to pdf barcode_pdf_oshandle, barcode_pdf_name = tempfile.mkstemp(suffix='.pdf') cmd = [GHOSTSCRIPT_PATH, '-q', '-dNOPAUSE', '-dBATCH', '-sDEVICE=pdfwrite', '-sPAPERSIZE=a4', '-dAutoRotatePages=/None', '-sOutputFile=%s' % barcode_pdf_name, '-c', '<</Orientation 0>> setpagedevice', '-'] popen = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = popen.communicate(barcode_ps) if popen.returncode != 0: raise IOError('barcode processing using ghostscript returned error code %i , stderr: %s' % (popen.returncode, stderr)) finally: os.close(barcode_pdf_oshandle) # implant barcode pdf to source pdf on every page source_filelike.seek(0) cmd = [which('pdftk').next(), '-', 'stamp', barcode_pdf_name, 'output', '-', 'dont_ask'] popen = subprocess.Popen(cmd, bufsize=-1, stdin=source_filelike, stdout=dest_filelike, stderr=subprocess.PIPE) stdout, stderr = popen.communicate() source_filelike.seek(0) if os.path.isfile(barcode_pdf_name): os.remove(barcode_pdf_name) if popen.returncode != 0: raise IOError('stamping pipeline returned with errorcode %i , stderr: %s' % (popen.returncode, stderr))
# -*- coding: utf-8 -*- import os, subprocess, tempfile, binascii, logging from django.conf import settings from django.template import Context, loader from pdfminer.pdfparser import PDFParser, PDFDocument from pdfminer.pdftypes import PDFException import ecs.utils.killableprocess from ecs.utils.pathutils import which MONTAGE_PATH = which('montage').next() GHOSTSCRIPT_PATH = settings.ECS_GHOSTSCRIPT if hasattr(settings,"ECS_GHOSTSCRIPT") else which('gs').next() PDF_MAGIC = r"%PDF-" class Page(object): ''' Properties of a image of an page of an pageable media (id, tiles_x, tiles_y, width, pagenr) ''' def __init__(self, id, tiles_x, tiles_y, width, pagenr): self.id = id self.tiles_x = tiles_x self.tiles_y = tiles_y self.width = width self.pagenr = pagenr def __repr__(self): return str("%s_%s_%sx%s_%s" % (self.id, self.width, self.tiles_x, self.tiles_y , self.pagenr))
------------------ third-party: GnuPg ------------------ - The GNU Privacy Guard -- a free implementation of the OpenPGP standard as defined by RFC4880 - GnuPG is licensed """ import os, subprocess from django.conf import settings from ecs.utils.pathutils import which GPG_EXECUTABLE = settings.ECS_GNUPG if hasattr(settings,"ECS_GNUPG") else which('gpg').next() def reset_keystore(gpghome): ''' wipes out keystore under directory gpghome; Warning: deletes every file in this directory ''' if not os.path.isdir(gpghome): os.makedirs(gpghome) for f in os.listdir(gpghome): path = os.path.join(gpghome, f); if os.path.isfile(path): os.remove(path) def gen_keypair(ownername): ''' Returns a tuple of amored strings, first is secret key, second is publickey''' return NotImplementedError