sogwhite / flying-saucer

Automatically exported from code.google.com/p/flying-saucer
0 stars 0 forks source link

Allow a way for setting page size, page margins from Java code #258

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create MyPDFPageEvents class as below

public class MyPDFPageEvents extends PdfPageEventHelper {
    ...//not all methods are listed here
    public void onStartPage(PdfWriter writer, Document document) {
        document.setPageSize(PageSize.A5);
        document.setMargins(100, 100, 100, 100);
    }
    ...
}

2. Create FlyingSaucerPDFCreationListener class as below
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xhtmlrenderer.pdf.PDFCreationListener;

import test.pdf.MyPDFPageEvents;

public class FlyingSaucerPDFCreationListener implements PDFCreationListener {
    MyPDFPageEvents pdfPageEvents = null;

    public FlyingSaucerPDFCreationListener(MyPDFPageEvents mppe) {
        pdfPageEvents=mppe;
    }

    @Override
    public void preOpen(ITextRenderer iTextRenderer) {
        System.out.println("preOpen("+iTextRenderer+")");
        // set the PdfPageEvent instance to the PdfWriter
        iTextRenderer.getWriter().setPageEvent(pdfPageEvents);
    }

    @Override
    public void preWrite(ITextRenderer iTextRenderer, int pageCount) {
        System.out.println("preWrite("+iTextRenderer+", "+pageCount+")");
    }

    @Override
    public void onClose(ITextRenderer renderer) {
        System.out.println("onClose("+renderer+")");
    }

}

3. Call everything from another class's main method to generate PDF from HTML

public static void main(String[] args) throws IOException, DocumentException {
    InputStream is = FlyingSaucerPdfRenderer.class.getResourceAsStream("/sample-html.html");
    String fileHTML = getStringFromInputStream(is);

    String cleanedHTML = cleanHTML(fileHTML);

    // Create the PDF
    MyITextRenderer renderer = new MyITextRenderer();
    MyPDFPageEvents mppe = new MyPDFPageEvents(renderer);
    FlyingSaucerPDFCreationListener fspcl = new FlyingSaucerPDFCreationListener(mppe);
    renderer.setListener(fspcl);
    renderer.setDocumentFromString(cleanedHTML);
    renderer.layout();
    OutputStream outputStream = new FileOutputStream("FlyingSaucer-HTMLtoPDF.pdf");
    renderer.createPDF(outputStream);

    // Finishing up
    renderer.finishPDF();
}

What is the expected output? 
- A PDF generated with A5 page size

What do you see instead?
- A PDF generated with the default A4 page size

What version of the product are you using? 
- flying-saucer-core-9.0.7.jar
- flying-saucer-pdf-9.0.7.jar
- iText-2.1.5.jar

On what operating system?
- Windows 7

Please provide any additional information below.
- Want to use HTML formatting except the page size in generated PDF
- Wanted a feature using which we can override page size, margins of CSS and 
pass it through the Java code. And flying saucer library should use this page 
information while generating PDF.

Original issue reported on code.google.com by j4jite...@gmail.com on 10 Mar 2015 at 7:14

GoogleCodeExporter commented 9 years ago
One important requirement in above is to be able to specify different page size 
and margin for each page from Java code.
e.g. Page 1: size=A5, Margin=100,100,100,100 and Page 2: size=A3, 
Margin=150,50,50,200
Updated code for MyPDFPageEvents 
public class MyPDFPageEvents extends PdfPageEventHelper {
    ...//not all methods are listed here
    public void onStartPage(PdfWriter writer, Document document) {
        Rectangle pageSize = PageSize.A4;
        float ml = 50, mr = 50, mt = 50, mb = 50;
        int pageNo = document.getPageNumber() % 5;
        switch (pageNo) {
        case 1:
            pageSize = PageSize.A4;
            ml = 200;
            mr = 100;
            mt = 200;
            mb = 100;
            break;
        case 2:
            pageSize = PageSize.A4;
            pageSize = pageSize.rotate();// portrait to landscape
            ml = 250;
            mr = 250;
            mt = 50;
            mb = 50;
            break;
        case 3:
            ...// more page configurations...
            break;
            ...// more page configurations...
        default:
            pageSize = PageSize.A4;
            ml = 50;
            mr = 50;
            mt = 50;
            mb = 50;
        }
        document.setPageSize(pageSize);
        document.setMargins(ml, mr, mt, mb);
    }
    ...
}

Original comment by j4jite...@gmail.com on 10 Mar 2015 at 8:09