Skip to content

Instantly share code, notes, and snippets.

@mtrebron

mtrebron/treepoem_pdf.py

Last active Nov 18, 2019
Embed
What would you like to do?
Treepoem save barcode as pdf
# -*- encoding:utf-8 -*-
from treepoem.__init__ import _format_code, _get_bbox, EPS_TEMPLATE, BWIPP, _get_ghostscript_binary
from treepoem.data import BarcodeType, barcode_types
import subprocess
def generate_ps_barcode(barcode_type, data, options=None):
"""generate postscript barcode, similar to treepoem.generate_barcode()
"""
if barcode_type not in barcode_types:
raise NotImplementedError('unsupported barcode type {!r}'.format(barcode_type))
if options is None:
options = {}
code = _format_code(barcode_type, data, options)
bbox_lines = _get_bbox(code)
full_code = EPS_TEMPLATE.format(bbox=bbox_lines, bwipp=BWIPP, code=code)
# return the raw ps barcode
return full_code.encode('utf-8')
def save_ps_as_pdf(ps_code, pdf_path):
""" convert ps to pdf and save using the gs pdfwrite device
using the same code structure as in treepoem _get_bbox()
"""
ghostscript = _get_ghostscript_binary()
gs_process = subprocess.Popen(
[ghostscript, '-sDEVICE=pdfwrite', '-dBATCH', '-dSAFER', '-dUseCropBox=true', '-dFitPage', '-sOutputFile={pdf_path}'.format(pdf_path=pdf_path), '-'],
universal_newlines=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
_, error_output = gs_process.communicate(ps_code)
error_output = error_output.strip()
return error_output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.