2012-10-29 14 views
5

Rapor raporunu kullanarak, bir dizi qr kodu nasıl oluşturabilirim ve bunları bir pdf'ye nasıl yerleştirebilirim ve sonra da kullanıcı tarayıcısında açabilirim. İşte benim girişimim. Şimdiden teşekkürler. Aşağıdaki bu kod için hiçbir şey olmuyor. Pdf dosyasını kaydetmem istenmesi bekleniyordu.Tek bir pdf dosyasında birden çok qr kodu raporlab ve django çerçevesini kullanarak oluşturun

from reportlab.pdfgen import canvas 
from django.http import HttpResponse 
from reportlab.graphics.shapes import Drawing 
from reportlab.graphics.barcode.qr import QrCodeWidget 
from reportlab.graphics import renderPDF 
# Create the HttpResponse object with the appropriate PDF headers. 
response = HttpResponse(mimetype='application/pdf') 
response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"' 

p = canvas.Canvas(response) 

qrw = QrCodeWidget('Helo World!') 
b = qrw.getBounds() 

w=b[2]-b[0] 
h=b[3]-b[1] 

d = Drawing(45,45,transform=[45./w,0,0,45./h,0,0]) 
d.add(qrw) 

renderPDF.draw(d, p, 1, 1) 

p.showPage() 
p.save() 
return response 
+0

üzerinden okuma yapmanızı öneririz. Sorunlarınızı daha spesifik bir şekilde açıklayabilir misiniz? Kodunuz doğru/yanlış, tam olarak nerede zorluklarla karşılaşıyorsunuz? – Rytmis

cevap

4

Sizin kodunuz benim için çalıştı, ancak bunun bir görünümde kapsamadığınızdan şüpheleniyorum. söylemek benim tarayıcı Açılış Örneğin

, myapp/views.py

from reportlab.pdfgen import canvas 
from django.http import HttpResponse 
from reportlab.graphics.shapes import Drawing 
from reportlab.graphics.barcode.qr import QrCodeWidget 
from reportlab.graphics import renderPDF 


# Create your views here. 
def test_qr(request): 
    # Create the HttpResponse object with the appropriate PDF headers. 
    response = HttpResponse(mimetype='application/pdf') 
    response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"' 

    p = canvas.Canvas(response) 

    qrw = QrCodeWidget('Helo World!') 
    b = qrw.getBounds() 

    w=b[2]-b[0] 
    h=b[3]-b[1] 

    d = Drawing(45,45,transform=[45./w,0,0,45./h,0,0]) 
    d.add(qrw) 

    renderPDF.draw(d, p, 1, 1) 

    p.showPage() 
    p.save() 
    return response 

myproject/urls.py

from django.conf.urls.defaults import patterns, include, url 

urlpatterns = patterns('', 
    url(r'^$', 'myapp.views.test_qr'), 
) 

http: 127.0.0.1: 8000 pdf indirmek için beni ister sol alt köşede bir QR kodu ile işlenir. Django'yu nasıl kullanacağınızdan emin değilseniz, Django Book Online

+0

Onların tarayıcı uyumluluk sorunu olduğunu düşünüyorum. Firefox'ta çalışır ancak kromda (15) yoktur – user1783848

+1

Çalışmak için 'HttpResponse (mimetype =' application/pdf ') '-' HttpResponse (content_type =' application/pdf ') 'şeklinde değiştirmek zorunda kaldım – bjesus

İlgili konular