使用飞碟 (xHTMLRenderer) 和 iText 生成页眉/页脚

发布于 2024-12-18 02:13:35 字数 2731 浏览 2 评论 0原文

我意识到这个问题之前已经被问过(我查看了所有解决方案并尝试了所有解决方案),但我仍在尝试生成一个 pdf 文档,其中的页眉和页脚在每个页面上重复。

我正在使用 flying Sacer R8 和 iText2.0.8 我尝试了许多不同的方法来让它工作,但到目前为止没有效果。我测试过的一些方法是 https://gist.github.com/626264,使用运行元素和边距盒子http://pigeonholdings.com/projects/flyingsaucer/R8/doc/guide/users-guide-R8.html#xil_40(css3功能),飞碟r7的指南,不不适用于 r8 http://today.java.net/pub/a/today/2007/06/26/generate-pdfs-with-flying-saucer-and-itext.html#page-specific-features< /a>,还有许多其他方法,但对我来说不起作用。

我的页眉 div 包含另外 2 个带有图像的 div,而页脚仅用于页码编号。 html 被放入名为 buf 的 StringBuffer 中。

buf.append("<head>");
    buf.append("<title>blabla</title> ");
    buf.append("<style type='text/css' media='print'>  ");
    buf.append("@page { size:8.5in 11in; padding:1em; @bottom-left { content: element(footer); } } ");
    buf.append("#footer { font-size: 90%; font-style: italic;  position: running(footer); top: 0; left: 0; }");
    buf.append("#pagenumber:before { content: counter(page); } ");
    buf.append("#pagecount:before { content: counter(pages); } ");
buf.append("</style></head>");
buf.append("<body>");
 buf.append("<div class='header' style='clear:both;'>");
    buf.append("<div id='moneyLogo' style='float:left'>"); 
    buf.append("<img src='logo.jpg' alt='Some alt text' />");
    buf.append("</div>");
    buf.append("<div id='canLogo' style='float:right'>");
    buf.append("<img src='someImg.gif' alt='alt text' />");
    buf.append("</div>");
    buf.append("<h3 style='text-align:center; clear:both;'>alt text</h3>");
    buf.append("<div style='text-align:center;'>");
    buf.append("Some texy text");
    buf.append("<br />"););
    buf.append("</div>");
    buf.append("</div><br /><br />");
buf.append("<div id='footer'>  Page <span id='pagenumber'/> of <span id='pagecount'/> </div>");

    buf.append("</body>");
    buf.append("</html>");

我的 pdf 生成得很好,除了页眉仅出现在第一页而页脚仅出现在最后一页的底部。当我将 html 通过 w3c 验证器时,结果很好,但是当我使用他们的 CSS 验证器时,它说它们在行 @page { size:8.5in 11in; 中存在解析错误。填充:1em; @bottom-left { 内容:元素(页脚); 。

据我从我正在阅读的所有指南来看,这很好 我还听说 W3C CSS 验证器对于 CSS3 规范来说不完整,所以我认为验证器是错误的。

如果有人能给我一些关于在哪里查看或想法的提示,这将是我的一周:)

ps 必须使用飞碟 R8 和/或 iText 2.0.8

I realize this question has been asked before (I looked at all the solutions and tried them all) but I am still trying to generate a pdf document with a header and footer that repeat on every page.

I am using flying saucer R8 with iText2.0.8 I have tried many different methods to get it working but so far to no avail. Some methods I tested out were https://gist.github.com/626264, using running elements and margin boxs http://pigeonholdings.com/projects/flyingsaucer/R8/doc/guide/users-guide-R8.html#xil_40 (css3 feature), a guide for flying saucer r7 that doesn't work for r8 http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html#page-specific-features, a long with many other methods which did not work for me.

My header div contains 2 other divs with images and my footer is just for page numbering. The html is being put into a StringBuffer called buf.

buf.append("<head>");
    buf.append("<title>blabla</title> ");
    buf.append("<style type='text/css' media='print'>  ");
    buf.append("@page { size:8.5in 11in; padding:1em; @bottom-left { content: element(footer); } } ");
    buf.append("#footer { font-size: 90%; font-style: italic;  position: running(footer); top: 0; left: 0; }");
    buf.append("#pagenumber:before { content: counter(page); } ");
    buf.append("#pagecount:before { content: counter(pages); } ");
buf.append("</style></head>");
buf.append("<body>");
 buf.append("<div class='header' style='clear:both;'>");
    buf.append("<div id='moneyLogo' style='float:left'>"); 
    buf.append("<img src='logo.jpg' alt='Some alt text' />");
    buf.append("</div>");
    buf.append("<div id='canLogo' style='float:right'>");
    buf.append("<img src='someImg.gif' alt='alt text' />");
    buf.append("</div>");
    buf.append("<h3 style='text-align:center; clear:both;'>alt text</h3>");
    buf.append("<div style='text-align:center;'>");
    buf.append("Some texy text");
    buf.append("<br />"););
    buf.append("</div>");
    buf.append("</div><br /><br />");
buf.append("<div id='footer'>  Page <span id='pagenumber'/> of <span id='pagecount'/> </div>");

    buf.append("</body>");
    buf.append("</html>");

My pdf generates fine except for the fact that the header only appears on the first page and the footer only appears on the bottom of the last page. When I put the html through the w3c validator it came out fine, but when I used their CSS validator it said that their were parse errors in the line @page { size:8.5in 11in; padding:1em; @bottom-left { content: element(footer); } }

As far as I could tell from all the guides I was reading this was fine. I also heard that the W3C CSS validator was incomplete for CSS3 specs so I assumed it was the validator who was wrong.

If anyone could give me some tips of where to look or ideas it would make my week :)

p.s. Has to use Flying saucer R8 and/or iText 2.0.8

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

镜花水月 2024-12-25 02:13:35

这是一个工作示例:

package com.sg2net.test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.DocumentException;

public class XHTMLRenderer8 {

    /**
     * @author Giovanni Cuccu
     */
    public static void main(String[] args) throws FileNotFoundException, DocumentException {
        ITextRenderer renderer = new ITextRenderer();
        String content="<html><head><style>\n" +
          "div.header {\n" +
          "display: block; text-align: center;\n" + 
          "position: running(header);}\n" +
          "div.footer {\n" +
          "display: block; text-align: center;\n" + 
          "position: running(footer);}\n" +
          "div.content {page-break-after: always;}" +
          "@page { @top-center { content: element(header) }}\n " +
          "@page { @bottom-center { content: element(footer) }}\n" +
          "</style></head>\n" +
          "<body><div class='header'>Header</div><div class='footer'>Footer</div><div class='content'>Page1</div><div>Page2</div></body></html>";
        renderer.setDocumentFromString(content);
        renderer.layout();
        renderer.createPDF(new FileOutputStream("test.pdf"));

    }

}

这是使用以下 XHTML 文档

<html>
<head>
<style>
div.header {
    display: block; text-align: center; 
    position: running(header);
}
div.footer {
    display: block; text-align: center;
    position: running(footer);
}
div.content {page-break-after: always;}
@page {
     @top-center { content: element(header) }
}
@page { 
    @bottom-center { content: element(footer) }
}
</style>
</head>
<body>
    <div class='header'>Header</div>
    <div class='footer'>Footer</div>
    <div class='content'>Page1</div>
    <div>Page2</div>
</body>
</html>

Here is a working example:

package com.sg2net.test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.DocumentException;

public class XHTMLRenderer8 {

    /**
     * @author Giovanni Cuccu
     */
    public static void main(String[] args) throws FileNotFoundException, DocumentException {
        ITextRenderer renderer = new ITextRenderer();
        String content="<html><head><style>\n" +
          "div.header {\n" +
          "display: block; text-align: center;\n" + 
          "position: running(header);}\n" +
          "div.footer {\n" +
          "display: block; text-align: center;\n" + 
          "position: running(footer);}\n" +
          "div.content {page-break-after: always;}" +
          "@page { @top-center { content: element(header) }}\n " +
          "@page { @bottom-center { content: element(footer) }}\n" +
          "</style></head>\n" +
          "<body><div class='header'>Header</div><div class='footer'>Footer</div><div class='content'>Page1</div><div>Page2</div></body></html>";
        renderer.setDocumentFromString(content);
        renderer.layout();
        renderer.createPDF(new FileOutputStream("test.pdf"));

    }

}

This is using the following XHTML document

<html>
<head>
<style>
div.header {
    display: block; text-align: center; 
    position: running(header);
}
div.footer {
    display: block; text-align: center;
    position: running(footer);
}
div.content {page-break-after: always;}
@page {
     @top-center { content: element(header) }
}
@page { 
    @bottom-center { content: element(footer) }
}
</style>
</head>
<body>
    <div class='header'>Header</div>
    <div class='footer'>Footer</div>
    <div class='content'>Page1</div>
    <div>Page2</div>
</body>
</html>
長街聽風 2024-12-25 02:13:35

经过大量研究和测试后,我想出了一个真正有效的解决方案。

您可以控制:

- 通过编辑 margin-top 来控制标题高度;

- 通过编辑 margin-bottom 来调整页脚高度;

- 内容宽度通过编辑div.content宽度。

页码显示在页脚处。

请参阅下面的代码:

<html>
<head>
<style>

@page{

    @bottom-left {                 
        content: element(footer);  
        vertical-align: top;
        padding-top: 10px;
/*      border: solid red;    */
    }

    @top-right {
        content: element(header); 
        vertical-align: bottom;
        padding-bottom: 10px;
/*          border: solid green;   */
    }

    size: A4 portrait;
    margin-top:5.5cm; 
    margin-left:3cm; 
    margin-right:2cm; 
    margin-bottom:3.3cm; 
}

div.header {
    display: block;                     
    position: running(header);
    border-bottom: 1px solid black;
}

div.footer {
    margin-top: 0.5cm;
    display: block;
    position: running(footer);
    border-top: 1px solid black; 
}

div.content {
/*  border: solid purple;  */
    display: block;
    width: 15.4cm; 
    text-align: justify;
}

#pagenumber:before {
    content: counter(page);
}

#pagecount:before {
    content: counter(pages);
}

</style>
</head>
<body>

    <div class="header">
        This is the header that will repeat on every page at top
    </div>

    <div class="footer" >
        <p>This is the footer that will repeat on every page at bottom</p>
        <p>Page <span id="pagenumber"></span> of <span id="pagecount"></span></p>/
    </div>

    <div class="content">
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
    </div>

</body>
</html>

希望有帮助!

After researching and testing a lot, I just came up with a solution that really works.

You can control:

- header height by editing margin-top;

- footer height by editing margin-bottom;

- content width by editing div.content width.

Page numbers are displayed at footer.

See code below:

<html>
<head>
<style>

@page{

    @bottom-left {                 
        content: element(footer);  
        vertical-align: top;
        padding-top: 10px;
/*      border: solid red;    */
    }

    @top-right {
        content: element(header); 
        vertical-align: bottom;
        padding-bottom: 10px;
/*          border: solid green;   */
    }

    size: A4 portrait;
    margin-top:5.5cm; 
    margin-left:3cm; 
    margin-right:2cm; 
    margin-bottom:3.3cm; 
}

div.header {
    display: block;                     
    position: running(header);
    border-bottom: 1px solid black;
}

div.footer {
    margin-top: 0.5cm;
    display: block;
    position: running(footer);
    border-top: 1px solid black; 
}

div.content {
/*  border: solid purple;  */
    display: block;
    width: 15.4cm; 
    text-align: justify;
}

#pagenumber:before {
    content: counter(page);
}

#pagecount:before {
    content: counter(pages);
}

</style>
</head>
<body>

    <div class="header">
        This is the header that will repeat on every page at top
    </div>

    <div class="footer" >
        <p>This is the footer that will repeat on every page at bottom</p>
        <p>Page <span id="pagenumber"></span> of <span id="pagecount"></span></p>/
    </div>

    <div class="content">
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        <p>This is the content</p><p>This is the content</p><p>This is the content</p>
    </div>

</body>
</html>

Hope it helps!!!

三生殊途 2024-12-25 02:13:35

对于使用java的itext,请

public class HeaderAndFooter extends PdfPageEventHelper {

public void onEndPage (PdfWriter writer, Document document) {
    Rectangle rect = writer.getBoxSize("art");
    switch(writer.getPageNumber() % 2) {
    case 0:
        ColumnText.showTextAligned(writer.getDirectContent(),
                Element.ALIGN_RIGHT, new Phrase("even header"),
                rect.getBorderWidthRight(), rect.getBorderWidthTop(), 0);
        break;
    case 1:
        ColumnText.showTextAligned(writer.getDirectContent(),
                Element.ALIGN_CENTER, new Phrase(String.format("%d", writer.getPageNumber())),
                300f, 62f, 0);
        break;
    }
    ColumnText.showTextAligned(writer.getDirectContent(),
            Element.ALIGN_CENTER, new Phrase(String.format("%d", writer.getPageNumber())),
            (2f + 4f) / 2, 2f - 18, 0);
}
}

使用您的pdfwriter中的以下一个进行检查

HeaderAndFooter event = new HeaderAndFooter();
        writer.setPageEvent(event);

for itext using java checkout this

public class HeaderAndFooter extends PdfPageEventHelper {

public void onEndPage (PdfWriter writer, Document document) {
    Rectangle rect = writer.getBoxSize("art");
    switch(writer.getPageNumber() % 2) {
    case 0:
        ColumnText.showTextAligned(writer.getDirectContent(),
                Element.ALIGN_RIGHT, new Phrase("even header"),
                rect.getBorderWidthRight(), rect.getBorderWidthTop(), 0);
        break;
    case 1:
        ColumnText.showTextAligned(writer.getDirectContent(),
                Element.ALIGN_CENTER, new Phrase(String.format("%d", writer.getPageNumber())),
                300f, 62f, 0);
        break;
    }
    ColumnText.showTextAligned(writer.getDirectContent(),
            Element.ALIGN_CENTER, new Phrase(String.format("%d", writer.getPageNumber())),
            (2f + 4f) / 2, 2f - 18, 0);
}
}

using below one in ur pdfwriter

HeaderAndFooter event = new HeaderAndFooter();
        writer.setPageEvent(event);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文