在PDF中查看时,我的行中出现换行.(添加减号时)



我在查看PDF格式的html页面时遇到问题。问题是,当数值太大时,它会打断减号和值之间的线。

这里是一个hmtl页面的例子,它运行得很好在此处输入图像描述

然后,当我将其转换为PDF时,它看起来是这样的:在此处输入图像描述

在这里,我将我的页面呈现为一个完整的html文档:

def document_to_html_document(document):
"""
Renders the invoice to a full html document with <html>, <head>, and <body> tags.
"""
return render_to_string('invoice_print.html', {
    'body': document_to_html(document),
    'document': document,
    'base_url': settings.SITE_BASE_URL
})

我构建HTML字符串并将其转换为pdf:的代码

def document_to_pdf(document, server_base_url, target=None):
    # Build HTML string
    html = document_to_html_document(document)
    # Setup input and output files
    html_file = tempfile.NamedTemporaryFile(suffix='.html')
    html_file.write(document_to_html_document(document).encode())
    target = target or tempfile.NamedTemporaryFile()
    # Convert to pdf
    command = 'xvfb-run wkhtmltopdf --footer-right "[page]/[topage]"{s}  {o}'.format(
    s=html_file.name,
    o=target.name
    )
    s = subprocess.Popen(command, shell=True)
    s.communicate()
   # Return pdf bytes
   target.seek(0)
   return target.read()

这是我的hmtl代码,我在其中添加了"body":

<html>
<head>
    <title>MOSEK-{{ document }}</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <!-- JQuery -->
    <script src="{{ base_url }}static/assets/vendor/jquery-1.11.1.js"></script>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="{{ base_url }}static/assets/vendor/bootstrap-3.1.1-dist/css/bootstrap.min.css">
    <!-- Optional theme -->
    <link rel="stylesheet" href="{{ base_url }}static/assets/vendor/bootstrap-3.1.1-dist/css/bootstrap-theme.css">
    <!-- Latest compiled and minified JavaScript -->
    <script src="{{ base_url }}static/assets/vendor/bootstrap-3.1.1-dist/js/bootstrap.min.js"></script>
    <style>
    body {
        width: 950px;
    }
    </style>
</head>
<body>
    <div>
        <div class="container">
            {{ body|safe }}
        </div>
    </div>
</body>

我不知道如何解决它,任何帮助都将不胜感激:)

这是Invoice_body:的内容

<div class="row">
        <div class="col-xs-12">
            <table class="invoice-lines">
                <thead>
                    <tr class="underline overline">
                        <th>No.</th>
                        <th>Part ID / Description</th>
                        <th class="nowrap fr sec-column">Quantity</th>
                        <th class="nowrap fr sec-column">List price</th>
                        <th class="nowrap fr sec-column">Unit price</th>
                        <th class="nowrap fr sec-column">Total</th>
                    </tr>
                </thead>
                <tbody>
                    {# We want the border-bottom of the last line to be darker. #}
                    {# In order to find this last line, we first need to determine #}
                    {# if there are custom items. If there are, then the last line #}
                    {# will be a custom item, since they are printed last. If not,  #}
                    {# then we need to find the last product line. #}
                    {% for line in lines %}
                    {% with lines|next:forloop.counter0 as next_line %}
                    <tr class="line{% if not citems and not next_line %} last-line{% endif %}">
                        <td>{{ line.line }}</td>
                        <td>
                            {% if line.objs.0.product.name %}
                                {{ line.objs.0.product.name }}<br />
                            {% else %}
                                {{ line.objs.0.serial.product.name }}-{% if line.objs.0.back == 1 %}BACK{% endif %}MAIN<br />
                            {% endif %}
                            {% if line.objs.0.back and line.objs.0.back_description != '' %}
                                {{ line.objs.0.back_description }}<br />
                            {% elif line.objs.0.back == False and line.objs.0.description != '' %}
                                {{ line.objs.0.description }}<br />
                            {% else %}
                                {{ line.objs.0.description }}<br />
                            {% endif %}
                            {% if line.objs.0.start %}
                                Period: {{ line.objs.0.start }} - {{ line.objs.0.end }}<br />
                            {% endif %}
                            Serial(s): {{ line.list }}
                        </td>
                        <td class="nowrap fr sec-column">{{ line.qty }}</td>
                        <td class="nowrap fr sec-column">{{ line.objs.0.price|floatformat:2 }}</td>
                        <td class="nowrap fr sec-column">
                            {{ line.objs.0.subtotal|floatformat:2 }}
                            {% if line.discount %}<br>({{ line.discount }}% dis.){% endif %}
                        </td>
                        <td class="nowrap fr sec-column">{{ line.rowtotal|floatformat:2 }}</td>
                    </tr>
                    {% endwith %}
                    {% endfor %}
                    {% for citem in citems %}
                    {% with citems|next:forloop.counter0 as next_citem %}
                        <tr class="line{% if not next_citem %} last-line{% endif %}">
                            <td>{{ citem.line }}</td>
                            <td>
                                {{ citem.obj.name }}
                                {% if citem.obj.agreement %}
                                    <br>Agreement: {{ citem.obj.agreement }}
                                {% endif %}
                            </td>
                            <td class="sec-column"> </td>
                            <td class="fr sec-column">{{ citem.rowtotal|floatformat:2 }}</td>
                            <td class="fr sec-column">{{ citem.rowtotal|floatformat:2 }}</td>
                            <td class="fr sec-column">{{ citem.rowtotal|floatformat:2 }}</td>
                        </tr>
                        {% endwith %}
                    {% endfor %}
                    <tr class="sum-line" id="subtotal-line">
                        <th colspan="4" class="fr">Subtotal</th>
                        <th> </th>
                        <td class="fr sec-column">{{ inv.subtotal|floatformat:2 }}</td>
                    </tr>
                    {% for vat, lines, message, total, rate in vats %}
                    <tr class="sum-line">
                        <td colspan="4" class="fr">
                            <span class="nowrap">Line {{ lines }} : </span> 
                            {{ message }}
                            <span class="nowrap"> ({{ rate }}% VAT)</span>
                        </td>
                        <td class="fr sec-column">{{ total }}</td>
                        <th> </th>
                    </tr>
                    {% endfor %}
                    <tr class="sum-line">
                        <th colspan="4" class="fr">VAT total</th>
                        <th> </th>
                        <td class="fr sec-column">{{ inv.vat|floatformat:2 }}</td>
                    </tr>
                    <tr class="sum-line">
                        <th colspan="4" class="fr">Total ({{ inv.currency }})</th>
                        <th> </th>
                        <td class="fr grand-total overline sec-column">{{ inv.total|floatformat:2 }}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

将标志缩放添加到导出命令:

command= xvfb-run wkhtmltopdf --zoom 1.5 --footer-right

Eliethesaiyan的解决方案:

在命令=xvfb中运行wkhtmltopdf--zoom 1.5--页脚右侧

最新更新