从HTML转换时,itextpdf 5.5.1的PDF内容解析问题 [英] itextpdf 5.5.1 Issue with PDF content resolution when converted from HTML

查看:195
本文介绍了从HTML转换时,itextpdf 5.5.1的PDF内容解析问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java中的ItextPdf 5.5.1和XMLWorker 5.5.1进行HTML到PDF的转换. 我设法将PDF文档的高度转换为HTML内容,但是PDF的内容看起来更大并且行之间有多余的空格.这些空格在HTML文档中不存在.

i am working in HTML to PDF conversion using ItextPdf 5.5.1 and XMLWorker 5.5.1 in Java. i managed to convert PDF document having height as that of HTML contents but contents in PDF looking bigger and having unwanted spaces between lines. These spaces are not there in HTML document.

private static void createPdf() {

    try {
        // getting HTML file from the path
        InputStream is = new FileInputStream(new File("/Users/salman.nazir/Desktop/html/tq.txt"));
        Date now = new Date();

        File file = new File(("/Users/salman.nazir/Desktop"), "my_" + now.getTime() + ".pdf");

        ElementList el = parseToElementList(is, new XMLWorkerFontProvider("resources/fonts/"));

        // width of 204pt
        float width = 204;

        // height as 10000pt (which is much more than we'll ever need)
        float max = 10000;

        //column without a `writer`
        ColumnText ct = new ColumnText(null);

        ct.setSimpleColumn(new Rectangle(width, max));
        for (Element e : el) {


            // Add only HTML Body Element
            // Avoiding IllegalArgumentException ("Format not found.")

            if(!e.isContent()) {
                System.out.print("META DATA");
            }
            else {
                ct.addElement(e);
            }
        }
        ct.go(true);

        // Getting y posItion from simulation mode
        float y = ct.getYLine();
        Rectangle pagesize = new Rectangle(width, (max - y) + 25);
        // Document with predefined page size
        Document document = new Document(pagesize, 0, 0, 0, 0);
        // Getting PDF Writer
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        // Column with a writer
        ct = new ColumnText(writer.getDirectContent());
        ct.setSimpleColumn(pagesize);
        for (Element e : el) {

            // Add only HTML Body Element
            // Avoiding IllegalArgumentException ("Format not found.")

            if(!e.isContent()) {
                System.out.print("META DATA");
            }
            else {
                ct.addElement(e);
            }
        }
        ct.go();
        // closing the document
        document.close();

        showPDFPath(file.getAbsolutePath());

    } catch (Exception e) {
        e.printStackTrace();

    }
}

在代码中的任何地方都可以设置分辨率吗?这是可以在浏览器中正常工作的HTML代码.

is there any thing to set resolution anywhere in the code ? here is HTML code that is working fine in browser.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="de">
<head>
    <title>Lieferschein/Rechnung 27.03.17 11:18 2017/2432</title>


    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <style type="text/css">
        @font-face {
    font-family: "Roboto Mono";
    src: url('RobotoMono-Bold.ttf') format('ttf'), url('RobotoMono-BoldItalic.ttf') format('ttf'), url('RobotoMono-Italic.ttf') format('ttf'), url('RobotoMono-Light.ttf') format('ttf'), url('RobotoMono-LightItalic.ttf') format('ttf'), url('RobotoMono-Medium.ttf') format('ttf'), url('RobotoMono-MediumItalic.ttf') format('ttf'), url('RobotoMono-Regular.ttf') format('ttf'), url('RobotoMono-Thin.ttf') format('ttf'), url('RobotoMono-ThinItalic.ttf') format('ttf');
}
body {
    font-family: "Roboto Mono";
    font-size: 2pt;
    width: 100%;
    margin: 0pt;
}

.documentType {
    text-transform: uppercase;
}

        h1 {
    text-align: center;
    font-size: 16pt;
    font-weight: normal;
}
h2 {
    text-align: center;
    font-size: 10pt;
    font-weight: normal;
    margin: 0pt;
}
tr.manual_imprint td {
    border-bottom:1pt dotted black;
    height: 30pt;
    vertical-align: bottom;
}
h3 {
    text-align: center;
    font-size: 13pt;
    font-weight: normal;
}
h3.left {
    font-size: 13pt;
    text-align: left;
}
hr {
    height: 1pt;
    color: black;
    background-color: black;
    border: 0pt;
}
table {
    width: 100%;
    border: 0pt;
    padding: 0pt;
    border-spacing: 0pt;
}
tr.lineitem_head td {
    border-bottom:1pt solid black;
}
tr.total td {
    border-top:1pt solid black;
    border-bottom:3pt double black;
    font-size: 6pt;
    font-weight: bold;
}
td {
    overflow: hidden;
}
td.left {
    max-width: 1px;
    text-align: left;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
}
td.left_indent {
    text-align: left;
    padding-left: 7pt;
}
td.right {
    text-align: right;
    vertical-align: top;
    white-space: nowrap;
}
.image-container {
  display: flex;
  justify-content: center;
}

    </style>
</head>

<body>
<h1>Tischlerei Helmut Meyer_676647</h1>

<h2>Winsener Landstrasse 22</h2>
<br></br>
<div class="image-container"><img src="http://www.iconsdb.com/icons/download/gray/android-6-512.jpg"/> </div>   <h2>21423 Winsen / Luhe</h2>
<h2></h2>
<h2>Tel.: +4940441777</h2>



<h3 class="left documentType">Lieferschein/Rechnung</h3>

<table class="order">


    <tr class="lineitem_head">

        <td>Nr. 2017/2432</td>

        <td class="right">27.03.17 11:18</td>
    </tr>
</table>

<table class="lineitems">
    <colgroup>
        <col width="100%" />
        <col width="0%" />
    </colgroup>

    <tbody>

    <tr class="lineitem" data-net="3,78 €">

        <td class="left">1x&nbsp;Filter Kalita</td>
        <td class="right">4,50 €</td>

    </tr>


    <tr class="lineitem" data-net="3,03 €">

        <td class="left">1x&nbsp;Latte</td>
        <td class="right">3,60 €</td>

    </tr>


    <tr class="lineitem" data-net="7,38 €">

        <td class="left">1x&nbsp;Skywalker/250g</td>
        <td class="right">7,90 €</td>

    </tr>


    <tr class="lineitem" data-net="8,32 €">

        <td class="left">1x&nbsp;Playground Love</td>
        <td class="right">8,90 €</td>

    </tr>


    <tr class="lineitem" data-net="12,06 €">

        <td class="left">1x&nbsp;Dschaggah Khan</td>
        <td class="right">12,90 €</td>

    </tr>


    <tr class="lineitem" data-net="12,06 €">

        <td class="left">1x&nbsp;King Kongo</td>
        <td class="right">12,90 €</td>

    </tr>


    </tbody>

    <tfoot>
    <tr class="total">
        <td class="left">Total</td>
        <td class="right">50,70 €</td>
    </tr>
    <tr class="net">
        <td class="left">Netto</td>
        <td class="right">46,62 €</td>
    </tr>




    <tr class="tax">
        <td class="left">7,00 VAT</td>
        <td class="right">2,79 €</td>
    </tr>

    <tr class="tax">
        <td class="left">19,00 VAT</td>
        <td class="right">1,29 €</td>
    </tr>

    </tfoot>
</table>


<h3>Vielen Dank für Ihren Besuch!</h3>

<h2>St-Nr.: </h2>



</body>
</html>

推荐答案

您的问题是由于解析器中使用的打印分辨率,默认情况下为72.您应设计该分辨率而不是100或任何其他分辨率(即:如果要打印字母大小(8.5英寸x 11英寸)的PDF,而不是设计850px x 1100px,则应在612px x 792px上进行打印.

Your issue is due to print resolution used in the parser which, by default is 72. You should design for that resolution instead of 100 or any other resolution (ie: If you'll be printing PDFs of letter size (8.5in x 11in), instead of designing for 850px by 1100px, you should do it on 612px x 792px.

这篇关于从HTML转换时,itextpdf 5.5.1的PDF内容解析问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆