HTML to PDF / DOCX / RTF Java converter library Forums PD4ML v3 Archived Forums (Read Only) Troubleshooting Urgent -Bad pdf layout – it span only 1/4 of normal width

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
  • #26646


    In our product, PD4ML is being used to generate pdf report from html.
    One of our customers generated the report which is very badly formated (although html file looks fine) – On each page, it spans 1/4 portion of page on the left, while other 3/4 page on the right is blank.

    I am not sure how to attach screenshot here but if you give me a email address, I can send you screenshot.

    Below is the snippet of code that I used PD4ML to generate pdf:

    public InputStream render(List pages, Map reportMetadata)
    throws RenderingException {

    boolean landscapeView = false;
    if (reportMetadata.containsKey(ReportMetadata.ORIENTATION)
    && “landscape”.equalsIgnoreCase(reportMetadata.get(ReportMetadata.ORIENTATION).toString()))
    landscapeView = true;

    PD4ML converter = new PD4ML();
    converter.setPageSize( landscapeView ? converter.changePageOrientation( PD4Constants.LETTER ): PD4Constants.LETTER );
    converter.setPageInsets(new Insets(20, 50, 10, 10));



    try {
    File report = File.createTempFile(“pdfreport”, “.pdf”);

    List pdfs = new ArrayList();
    File pdf;

    // for each page
    for (InputStream page : pages) {

    // create PDF
    pdf = File.createTempFile(“pdfreportpage”, “.pdf”);
    converter.render(new InputStreamReader(page),
    new FileOutputStream(pdf));

    PDFUtils.merge(pdfs, report);
    for(File tmpFile :pdfs)
    return new FileInputStream(report);

    } catch (IOException e) {
    throw new RenderingException(e);
    } catch (DocumentException e) {
    throw new RenderingException(e);


    Attach generated pdf


    try again to attach screenshot


    It looks like htmlWidth conversion parameter value (frame width of virtual web browser) is set too big.

    Also there is some garbage characters: it is caused by an invalid or missing charset directive in the source document.

    The document HTML source and resulting PDF would help to analyze the issue. Please publish or send to support pd4ml com


    I replaced “converter.adjustHtmlWidth()” with “convert.fitPageVertically()”, then everything starts to work fine.
    Is it safe to do so? what’s differene between these 2 method calls?


    Now I see what was the problem reason: I paid no heed to converter.adjustHtmlWidth() call.

    It is better to remove both: converter.adjustHtmlWidth() and convert.fitPageVertically() until you have no special need in the features.

    convert.fitPageVertically() tries to place all content to a single page. It does not reflow it, but just scales down content until it fit. If needed scale factors make text extremely tiny, it gives up. Obviously it happens with the document with your current configuration.

    converter.adjustHtmlWidth() first renders page with given converter.setHtmlWidth() (virtual web browser frame width), after that calculates right content edge, sets htmlWidth parameter to the new value and re-renders the page. But if the page has an element with width=100% the approach does not work as the right content edge will be always equal to the initial converter.setHtmlWidth() value.

    BTW: the code


    is not correct. As converter.setHtmlWidth() expects a parameter given in screen pixels, but PD4Constants.LETTER.width is a paper format in typographical points. Well, they are both integer values – hopefully you know what you do.


    Thanks for the explanation.

    The reason that I add “converter.adjustHtmlWidth()” is to try to fix another issue reported by customer — sometimes, the content (or table column) was chopped off on the page right side.

    Adding “converter.adjustHtmlWidth()” solves that issue. Unfortunately it causes current issue in certain pdf report -3/4 of page shows blank.


    > it causes current issue in certain pdf report -3/4 of page shows blank

    It should happen only in situations, when the source HTML has elements with width=”100%”, which leave no “blank space” to be cut with adjustHtmlWidth()

Viewing 8 posts - 1 through 8 (of 8 total)

The forum ‘Troubleshooting’ is closed to new topics and replies.