def test_zpl_to_zpl(self): zpl = GRF.replace_grfs_in_zpl(self._read_file('pdf-asciihex.zpl')) self._compare(zpl, 'asciihex-optimised-zb64.zpl') grf = GRF.from_zpl(self._read_file('pdf-asciihex.zpl'))[0] grf.optimise_barcodes() self._compare(grf.to_zpl(compression=1), 'pdf-optimised-b64.zpl') self._compare(grf.to_zpl(compression=2), 'pdf-optimised-asciihex.zpl') self._compare(grf.to_zpl(compression=3), 'pdf-optimised-zb64.zpl')
def test_ghostscript_center_of_pixel(self): grf = GRF.from_pdf( self._read_file('pdf.pdf'), 'TEST', center_of_pixel=True )[0] output = BytesIO() grf.to_image().save(output, 'PNG') self._compare(output.getvalue(), 'pdf-image-centerofpixel.png')
def test_ghostscript_center_of_pixel(self): grf = GRF.from_pdf(self._read_file('pdf.pdf'), 'TEST', center_of_pixel=True)[0] output = BytesIO() grf.to_image().save(output, 'PNG') self._compare(output.getvalue(), 'pdf-image-centerofpixel.png')
def test_pdf_to_image_multiple_pages(self): grfs = GRF.from_pdf(self._read_file('pdf-2pages.pdf'), 'TEST') self.assertEqual(len(grfs), 2) for i, grf in enumerate(grfs): output = BytesIO() grf.to_image().save(output, 'PNG') self._compare(output.getvalue(), 'pdf-2pages-%i.png' % i)
def test_pdf_to_image_using_bindings(self): grfs = GRF.from_pdf(self._read_file('pdf-2pages.pdf'), 'TEST', use_bindings=True) self.assertEqual(len(grfs), 2) for i, grf in enumerate(grfs): output = BytesIO() grf.to_image().save(output, 'PNG') self._compare(output.getvalue(), 'pdf-2pages-%i.png' % i) grf = GRF.from_pdf(self._read_file('pdf.pdf'), 'TEST', center_of_pixel=True, use_bindings=True)[0] output = BytesIO() grf.to_image().save(output, 'PNG') self._compare(output.getvalue(), 'pdf-image-centerofpixel.png')
def test_pdf_to_image_using_bindings(self): grfs = GRF.from_pdf( self._read_file('pdf-2pages.pdf'), 'TEST', use_bindings=True ) self.assertEqual(len(grfs), 2) for i, grf in enumerate(grfs): output = BytesIO() grf.to_image().save(output, 'PNG') self._compare(output.getvalue(), 'pdf-2pages-%i.png' % i) grf = GRF.from_pdf( self._read_file('pdf.pdf'), 'TEST', center_of_pixel=True, use_bindings=True )[0] output = BytesIO() grf.to_image().save(output, 'PNG') self._compare(output.getvalue(), 'pdf-image-centerofpixel.png')
def test_zpl_to_image(self): grf = GRF.from_zpl(self._read_file('pdf-asciihex.zpl'))[0] output = BytesIO() grf.to_image().save(output, 'PNG') self._compare(output.getvalue(), 'pdf-image.png') output = BytesIO() grf.optimise_barcodes() grf.to_image().save(output, 'PNG') self._compare(output.getvalue(), 'pdf-optimised-image.png')
def test_pdf_to_image(self): grf = GRF.from_pdf(self._read_file('pdf.pdf'), 'TEST')[0] output = BytesIO() grf.to_image().save(output, 'PNG') self._compare(output.getvalue(), 'pdf-image.png') output = BytesIO() grf.optimise_barcodes() grf.to_image().save(output, 'PNG') self._compare(output.getvalue(), 'pdf-optimised-image.png')
def get_grf(png, rotate): u"""Get a GRF from a png. params: png: BytesIO stream rotate: rotate 90° if true """ if rotate: png_bytes = BytesIO() (Image.open(png).rotate(90, expand=True).save(png_bytes, format="PNG")) png = png_bytes.getvalue() png_bytes.close() return GRF.from_image(png, "DEMO")
def get_grf(png, rotate): u"""Get a GRF from a png. params: png: BytesIO stream rotate: rotate 90° if true """ if rotate: png_bytes = BytesIO() (Image .open(png) .rotate(90, expand=True) .save(png_bytes, format="PNG")) png = png_bytes.getvalue() png_bytes.close() return GRF.from_image(png, 'DEMO')
def print(weight, public_price, price_pakke): try: client = pdfcrowd.HtmlToImageClient( 'JosepRoo', 'd508735c4b86f87fd4a3961d5195126b') # configure the conversion client.setOutputFormat('png') # run the conversion and store the result into an image variable image = client.convertString( set_html(weight, public_price, price_pakke)) temp_buff = BytesIO() temp_buff.write(image) # need to jump back to the beginning before handing it off to PIL temp_buff.seek(0) image = Image.open(temp_buff) # image = image.resize([720, 1080]) next resolution image = image.resize([830, 1133]) temp_buff.seek(0) image.save(temp_buff, format='PNG') grf = GRF.from_image(temp_buff.getvalue(), 'ZPL') grf.optimise_barcodes() import socket mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = "169.254.239.30" port = 6101 print(grf.to_zpl(compression=3, quantity=1)) try: mysocket.connect((host, port)) # connecting to host mysocket.send( bytes(grf.to_zpl(compression=3, quantity=1), "utf-8")) # using bytes mysocket.close() # closing connection except: print("Error with the connection") # Some random options except pdfcrowd.Error as why: # report the error sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) # handle the exception here or rethrow and handle it at a higher level raise
def get_zpl_data(self, qweb_url, printer_name=False): try: data = [] qweb_source_parts = qweb_url.split('/') report = self.search([('report_name', '=', str(qweb_source_parts[-2]))])[0] if report.use_template: report_template_id = report.report_template_id if report_template_id: template_text = report_template_id.template_text or "" model_name = report.model model_id = qweb_source_parts[-1] if model_id.find(','): model_ids = model_id.split(',') model_ids = list(map(lambda x: int(x), model_ids)) for m_id in model_ids: zpl_data = self.parse_template( template_text, model_name, m_id) data.append(zpl_data) return data reportname = qweb_source_parts[-2] docids = qweb_source_parts[-1] document = self.report_routes(reportname, docids=docids, converter="pdf") pages = GRF.from_pdf(document, 'DEMO') fd, file_name = mkstemp() with open(file_name, "w+") as f: temp = '' for grf in pages: grf.optimise_barcodes() zpl_line = grf.to_zpl() try: zpl_line = zpl_line.encode(encoding='UTF-8', errors='strict') except Exception as e: _logger.info("---------encoding--EXCEPTION---2---%r", e) finally: data.append(zpl_line) return data except Exception as e: raise Warning(e)
def test_pdf_landscape(self): grf = GRF.from_pdf(self._read_file('pdf-landscape.pdf'), 'TEST')[0] output = BytesIO() grf.to_image().save(output, 'PNG') self._compare(output.getvalue(), 'pdf-landscape.png')
def test_image_to_zpl(self): grf = GRF.from_image(self._read_file('pdf-image.png'), 'TEST') grf.optimise_barcodes() self._compare(grf.to_zpl(copies=2), 'image-optimised-zb64-copies2.zpl')
def test_from_b64_zpl(self): grf = GRF.from_zpl(self._read_file('pdf-optimised-zb64.zpl'))[0] self._compare(grf.to_zpl(compression=2), 'pdf-optimised-asciihex.zpl') output = BytesIO() grf.to_image().save(output, 'PNG') self._compare(output.getvalue(), 'pdf-optimised-image.png')
def main(prefix="*:", rows=1, cols=3): # Se crea la conexión con cups conn = cups.Connection() # Se obtiene el nombre de la impresora a usar printer_name = args.printer_name # Archivo temporal a enviar como raw label_file_path = "/tmp/label.zpl" # Se revisar si existe la impresora, en caso contrario se despliegan las disponibles if printer_name != "None": printers = conn.getPrinters() if printer_name not in printers.keys(): print("printer not found\n\navailable printer:") print("\n".join(printers.keys())) return False # Comenzamos el archivo zql vacío global_zpl = "" for idx in range(int(rows)): # Se verifica cuantas columnas se ingresó para crear el tamaño del qr if cols == 3: qr_size = 2 else: qr_size = 4 # Se obtiene el qr izquiedo grf_left = GRF.from_image(generate_qr(prefix, qr_size), "L" + str(idx)[:7]) grf_left.optimise_barcodes() qr_left = grf_left.to_zpl_line(compression=3, quantity=1) # Se obtiene el qr derecho grf_right = GRF.from_image(generate_qr(prefix, qr_size), "R" + str(idx)[:7]) grf_right.optimise_barcodes() qr_right = grf_right.to_zpl_line(compression=3, quantity=1) # Si es de 3 columnas, entonces es el qr pequeño if cols == 3: # Se crea un qr central grf_center = GRF.from_image(generate_qr(prefix, qr_size), "C" + str(idx)[:7]) grf_center.optimise_barcodes() qr_center = grf_center.to_zpl_line(compression=3, quantity=1) # Se posicionan los qr en la fila en diferente x zpl = [ qr_left, qr_right, qr_center, "^XA", "^LH32,8", "^XGR:L" + str(idx)[:7] + ".GRF,1,1", "^FS", "^LH172,8", "^XGR:C" + str(idx)[:7] + ".GRF,1,1", "^FS", "^LH312,8", "^XGR:R" + str(idx)[:7] + ".GRF,1,1", "^FS", "^XZ" ] else: # Si es de 2 columnas es el qr super y sólo necesita 2, es el default zpl = [ qr_left, qr_right, "^XA", "^LH0,20", "^XGR:L" + str(idx)[:7] + ".GRF,1,1", "^FS", "^LH214,20", "^XGR:R" + str(idx)[:7] + ".GRF,1,1", "^FS", "^XZ" ] global_zpl = global_zpl + "\n".join(zpl) + "\n" # Si viene la etiqueta no-print entonces se regresa el archivo zpl if printer_name == "None": print(global_zpl) else: f = open(label_file_path, "w") # Se guarda el archivo y se agrega el comando para eliminar las imagenes que se requirieron en la máquina f.write(global_zpl + "^XA^IDR:*.GRF^FS^XZ") f.close() # Se manda a imprimir como raw job_id = conn.printFile(printer_name, label_file_path, "Label Print", {"raw": "True"}) print(job_id)