【问题标题】:itextpdf 5.5.1 Issue with PDF content resolution when converted from HTMLitextpdf 5.5.1 从 HTML 转换时的 PDF 内容分辨率问题
【发布时间】:2018-01-14 13:31:11
【问题描述】:

我正在使用 Java 中的 ItextPdf 5.5.1 和 XMLWorker 5.5.1 进行 HTML 到 PDF 的转换。 我设法转换了高度与 HTML 内容相同的 PDF 文档,但 PDF 中的内容看起来更大并且行之间有不需要的空格。 HTML 文档中不存在这些空格。

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 代码。

<!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>

【问题讨论】:

  • 由于您的问题是关于 html 2 pdf,如果适用,您还应该与资源共享示例 html。
  • html代码更新
  • 我认为“分辨率”有任何问题。实际上,您正在创建纯文本 PDF,因此分辨率永远不会成为问题。会不会混淆了“决议”和“领先”的概念?
  • 如果它与分辨率无关,那么您在浏览器中检查过这个 html 吗?在浏览器上它根据 CSS 工作。但在 PDF 中,文本字体很大,元素之间的空间也更宽。
  • 这里的“领先”是什么意思?当我仔细检查我身边的所有内容时,它会创建具有更大文本的相同 PDF。你能告诉我我错的可能性吗?当我发布所有代码时。

标签: itext html2pdf xmlworker


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多