使用xhtml2pdf从html创建pdf

时间:2014-05-09 08:00:08

标签: python css django xhtml2pdf

我想从html文件创建一个pdf报告,我遇到了两个问题。 Allgnment和字体

首先我有以下

<style>

    @font-face {
        font-family: "DejaVuSansMono";
        src: url("DejaVuSansMono-Bold.ttf");
        font-weight: bold;
    }
    @font-face {
        font-family: "DejaVuSansMono";
        src: url("DejaVuSansMono-Oblique.ttf");
        font-style: italic, oblique;
    }
    @font-face {
        font-family: "DejaVuSansMono";
        src: url("DejaVuSansMono-BoldOblique.ttf");
        font-weight: bold;
        font-style: italic, oblique;
    }
    @font-face {
        font-family: "DejaVuSansMono";
        src: url("DejaVuSansMono.ttf ");
    }
    .table {
        border: 1px solid black;
        background-color:#FCF0FB;
        text-align: center;
        padding-top: 3px;
    }
    @page {
        margin: 1cm;
        margin-bottom: 2.5cm;
        size: a4 portrait;
        margin-top:1cm;
        @frame header {
            -pdf-frame-content: headerContent;
            -pdf-frame-border:1cm;
            height:2cm;
            top: 2cm;
            margin-left:1cm;
            margin-right: 1cm;
        }

        @frame main {
            top:4cm;
            margin-left:1cm;
            margin-right:1cm;
            height:10cm;
            -pdf-frame-border: 1cm;
        }

        @frame footer {
            -pdf-frame-content: footerContent;
            -pdf-frame-border: 1cm;
            bottom: 2cm;
            margin-left: 1cm;
            margin-right: 1cm;
            height:1cm;
        }
    }
    *, html{
        font-family: "DejaVuSansMono";
        font-style:normal;
        font-size 10px;

    }
    .margin-10{
        margin-bottom:10;
    }
</style>


<div class="header" id="headerContent">
    <h3>Header content</h3>

</div>
<div class="margin-10"></div>
<div class="container">
    <h3>Today's appointments</h3>
    <table class="table">
        <thead>
            <tr>
                <th>#</th>
                <th>Time</th>
                <th>Patient</th>
                <th>Comments</th>
                <th>Doctor</th>

            </tr>
        </thead>
        <tbody>
            {%for doctor, entries in today.items%}
                {%for entry in entries%}
                    <tr>
                        <td>{{forloop.counter}}</td>
                        <td>{{entry.start|date:"H:i"}}</td>
                        <td>{{entry.patient}}</td>
                        <td>{{entry.comment}}</td>
                        <td>{{entry.doctor}}</td>

                    </tr>
                {%endfor%}
            {%endfor%}
        </tbody>
    </table>
</div>
<div class="footer" id="footerContent">
    <p>Για το Ιατρείο <pdf:pagenumber></p>
</div>

我希望有一个页眉,然后是页脚。但是我创建的margin-10类并没有在header和html内容之间添加任何余量

在我的页脚中,我有一封希腊字母,上面写着希腊字母。这封信不会正常显示,但作为一个黑色方块。我在项目的媒体文件夹中添加了DejaVusans字体,但它仍然无法正常工作。

pdf创建代码

path = os.path.abspath(os.path.join(MEDIA_ROOT, 'fonts'))
print "Path: ", path
def fetch_resources(uri, rel):
    path = os.path.abspath(os.path.join(rel, uri))
    print uri
    print "Path:", path
    print 'rel: ', rel
    return path

def write_to_pdf(template, context_dict, filename=None):
    #template = Template(template_data)
    template = get_template(template)
    context = Context(context_dict)
    html = template.render(context)
    result = StringIO.StringIO()
    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), result, path=path, encoding="UTF-8")
    if not pdf.err:
       response = http.HttpResponse(content_type='application/pdf')
        #response['Content-Disposition'] = 'attachment; filename=%s.pdf' % filename
        response.write(result.getvalue())
        return response
    return http.HttpResponse('Problem creating PDF: %s' % cgi.escape(html))

这是从上面的印刷声明在终端中打印的内容

DejaVuSansMono-Bold.ttf
DejaVuSansMono-Oblique.ttf
DejaVuSansMono-BoldOblique.ttf
DejaVuSansMono.ttf 

就是它看到了字体。我做错了什么?

0 个答案:

没有答案