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
    Posts
  • #26646

    Hi,

    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));
    converter.enableImgSplit(false);
    converter.enableTableBreaks(true);

    if(landscapeView)
    converter.setHtmlWidth(1000);
    else
    converter.setHtmlWidth(PD4Constants.LETTER.width);

    converter.adjustHtmlWidth();

    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”);
    pdfs.add(pdf);
    converter.render(new InputStreamReader(page),
    new FileOutputStream(pdf));
    }

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

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

    #28746

    Attach generated pdf

    #28747

    try again to attach screenshot

    #28748

    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

    #28749

    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?

    #28750

    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

    [language=java:6nsht2sl]if(landscapeView)
    converter.setHtmlWidth(1000);
    else
    converter.setHtmlWidth(PD4Constants.LETTER.width);[/language:6nsht2sl]

    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.

    #28751

    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.

    #28752

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