2011-11-24 5 views
18

मैं इस सवाल का एहसास के साथ पाद लेख से पहले (मैं सब समाधान को देखा और उन सब की कोशिश की) कहा गया है लेकिन मैं अभी भी एक शीर्ष लेख और पाद के साथ एक पीडीएफ दस्तावेज़ उत्पन्न करने के लिए कोशिश कर रहा हूँ जो हर पृष्ठ पर दोहराना है।उत्पन्न हैडर/उड़न तश्तरी (xHTMLRenderer) और नीली लपटें

मैं flying saucer R8 का उपयोग कर रहा हूं iText2.0.8 के साथ मैंने इसे काम करने के लिए कई अलग-अलग तरीकों का प्रयास किया है लेकिन अब तक कोई फायदा नहीं हुआ है। कुछ तरीकों मैं बाहर का परीक्षण किया https://gist.github.com/626264 थे, चल तत्वों और मार्जिन boxs 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/generating-pdfs-with-flying-saucer-and-itext.html#page-specific-features के लिए काम नहीं करता है, उड़ान के लिए एक गाइड का उपयोग कर एक लंबे कई अन्य तरीकों जो मेरे लिए काम नहीं किया है।

मेरे हैडर div छवियों के साथ 2 अन्य divs होता है और मेरे पाद लेख सिर्फ पृष्ठ क्रमांकन के लिए है। एचटीएमएल को 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>"); 

मेरे पीडीएफ तथ्य यह है कि शीर्ष लेख केवल प्रथम पृष्ठ पर दिखाई देता है और पाद केवल पिछले पृष्ठ के तल पर दिखाई देता है के अलावा ठीक उत्पन्न करता है। जब मैं डब्ल्यू 3 सी वैलिडेटर के माध्यम से एचटीएमएल डालता हूं तो यह ठीक हो गया, लेकिन जब मैंने अपने सीएसएस वैधकर्ता का इस्तेमाल किया तो उसने कहा कि उनकी लाइन @page { size:8.5in 11in; padding:1em; @bottom-left { content: element(footer); } }

जहां तक ​​मैं सभी गाइडों से कह सकता हूं, मैं इसे पढ़ रहा था ठीक था। मैंने यह भी सुना है कि डब्ल्यू 3 सी सीएसएस वैधकर्ता CSS3 चश्मा के लिए अधूरा था इसलिए मुझे लगता है कि यह वैधकर्ता था जो गलत था।

किसी को भी मुझे देखने या विचारों यह मेरी सप्ताह :)

पश्चलेख बनाना होगा, जहां से कुछ सुझाव दे सकता है उड़न तश्तरी R8 और/या उपयोग करने के लिए है iText 2.0.8

+1

भविष्य के उपयोगकर्ताओं के लिए एक टिप्पणी छोड़ना: यदि फ्लायरिंगौसर के लिए सभी पृष्ठों पर पाद लेख नहीं चल रहा है तो सुनिश्चित करें कि पाद लेख तत्व सामान्य HTML पृष्ठों के विपरीत सामग्री से पहले है। आप नीचे दिए गए उदाहरण @Giovanni देख सकते हैं जो दिखाता है कि शीर्षलेख और पाद लेख सामग्री से पहले हैं। आपका कोड सही है, यह केवल हेडर और पाद लेख को सामग्री से पहले रखा जाना चाहिए और यह बहुत ही प्रतिद्वंद्वी है –

उत्तर

26

यहाँ एक काम कर उदाहरण है:

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")); 

    } 

} 

यह उपयोग कर रहा है निम्नलिखित एक्सएचटीएमएल दस्तावेज़

<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> 
+0

धन्यवाद, मैं इस विधि का उपयोग कर समाप्त हुआ और यह मेरे लिए पूरी तरह से काम किया :) – flyingCaffine

+0

यह वही है जो मैं ढूंढ रहा था। धन्यवाद! – Alex

1

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); 
+1

मैंने पहले ऐसा कुछ उपयोग किया था लेकिन यह मेरे फ्लाइंग सॉकर कोड के साथ बहुत अच्छी तरह से एकीकृत नहीं हुआ था। अभी भी उपयोगी है अगर आप केवल उड़ान रक्षक के बिना iText का उपयोग कर रहे हैं :) – flyingCaffine

10

शोध और परीक्षण के बाद, मैं बस एक समाधान के साथ आया जो वास्तव में काम करता है।

आप नियंत्रित कर सकते हैं:

- संपादन मार्जिन टॉप द्वारा हैडर ऊंचाई;

- संपादन 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> 

आशा है कि यह मदद करता है !!!

+0

धन्यवाद! समाधान मेरे लिए काम किया। –

संबंधित मुद्दे