2017-03-29 5 views
0

Ich mag eine Rechnung im PDF-Format mit Barcode erstellen, erste mybarcode.py Datei reportlabwie eine Rechnung im PDF mit Barcode in django erstellen

# mybarcode.py-Datei erstellen Barcode mit barecode erstellen

from reportlab.lib.units import mm 
    from reportlab.graphics.barcode import createBarcodeDrawing 
    from reportlab.graphics.shapes import Drawing, String 
    from reportlab.graphics.charts.barcharts import HorizontalBarChart 

    class MyBarcodeDrawing(Drawing): 
      def __init__(self, text_value, *args, **kw): 
       barcode = createBarcodeDrawing('Code128', value=text_value, barHeight=19*mm, humanReadable=True) 
       Drawing.__init__(self,barcode.width,barcode.height,*args,**kw) 
       self.add(barcode, name='barcode') 

Ansichten Funktion zum Erstellen von PDF mit Barcode.

# views.py

from reportlab.platypus import SimpleDocTemplate 
from reportlab.platypus.tables import Table 
cm = 2.54 

def print_pdf(request): 

    import mybarcode 
    product = Product.objects.get(id = id) 
    d = mybarcode.MyBarcodeDrawing(product.code) 
    binaryStuff = d.asString('jpg') 

    response = HttpResponse(content_type='application/pdf') 
    filename=somefilename.pdf' 
    elements = [] 
    doc = SimpleDocTemplate(response, rightMargin=0, leftMargin=6.5 * cm, topMargin=0.3 * cm, bottomMargin=0) 

    data=[("barcode", binaryStuff),("name", "some_name")] 
    table = Table(data) 
    elements.append(table) 
    doc.build(elements) 
    return response 

und immer diese Fehlermeldung: ungültiger Start-Byte

Antwort

0

Dies könnte Ihre Probleme lösen:

'utf8' Codec kann nicht Byte 0xff in Position 0 dekodieren
import codecs 

    f = codecs.open(dir+location, 'r', encoding='utf-8') 
    txt = f.read() 

von diesem Moment an txt ist im Unicode-Format und Sie können es überall in Ihrem Code verwenden.

Wenn Sie UTF-8-Dateien nach der Verarbeitung generieren tun:

f.write(txt.encode('utf-8')) 
Verwandte Themen