2
def create_pdf(pdf_data):
    filename = app.config['UPLOAD_FOLDER'] + "/file.pdf"
    f = file(filename, "wb")
    pdf = pisa.CreatePDF(StringIO(pdf_data.encode('utf-8')), f)
    return pdf

pdf = create_pdf(render_template('receipt.htm', entry=entry, filename=filename))

html 模板显示图像,但是当使用 xhtml2pdf 转换为 pdf 时,图像不显示!

4

1 回答 1

0

我认为这对你有用:

def fetch_resources(uri, rel):
    if uri.startswith(settings.MEDIA_URL):
        path = os.path.join(settings.MEDIA_ROOT,uri.replace(settings.MEDIA_URL, ""))
    elif uri.startswith(settings.STATIC_URL):
        path = os.path.join(settings.LOADING_STATIC_FOR_PDF,uri.replace(settings.STATIC_URL, ""))
    return path

def preview_pdf(request):
    context = RequestContext(request)
    if request.method == 'GET':
        context = {
            # sending data to html 
        }
        rendered_html = render_to_string("path/xxx.html", locals())
        template = get_template('path/xxx.html')
        html = template.render(context)
        result = StringIO.StringIO()
        file = open('test.pdf', "w+b")
        pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file, encoding='utf-8',link_callback=fetch_resources)
        file.seek(0)
        pdf = file.read()
        file.close()
        return HttpResponse(pdf, 'application/pdf')
于 2016-04-12T05:51:34.923 回答