1

I'm trying to generate a PDF file from HTML. I have two long URL in the HTML code. The problem is that just one URL (longURL2) is split to the next line. The other URL (longURL) exceeds the page margin. Could someone help me to figure out why iText does not split the other URL?

My code:

String longURL = "http://localhost/erp/process/see_process.jsp?vp=ATENDING_PROCESS_EMPLOYEE&tr=1493131180671041&ev=1520277948136217";
String longURL2 = "https://stackoverflow.com/questions/48422409/how-to-set-pdf-page-size-a4-when-we-use-itextrenderer-to-generate-pdf-from-thyme";

String htmlCode = 
              "<p align=\"right\">City Name - March, 5 \r\n2018.</p>"
            + "<p>&nbsp;</p>"
            + "<p>&nbsp;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sollicitudin consectetur vulputate. "
            + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Interdum et malesuada fames ac ante ipsum primis in faucibus."
            + "<br/><br/></p>"
            + "<p>" + longURL + "</p>"
            + "<p>" + longURL2 + "</p>"
            + "<p>&nbsp;</p>"
            + "<p>&nbsp;</p><center>"
            + "<p><b>My First Name \r\nLast Name</b><br>My Department</p>"
            + "</center>";

File dest = new File("C:/Temp/target_pdf_file.pdf");

PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
pdf.setDefaultPageSize(PageSize.A4);

ConverterProperties converterProperties = new ConverterProperties();
FontProvider dfp = new DefaultFontProvider(true, true, true);
converterProperties.setFontProvider(dfp);

HtmlConverter.convertToPdf(htmlCode, pdf, converterProperties);
tnas
  • 510
  • 5
  • 16

1 Answers1

0

This is the solution I've figured out. The problem is the characters used by iText to split lines. It doesn't use characters like "_". So, first of all, it is necessary to add this character. A good approach is to extend the class DefaultSplitCharacters. The code is as follow:

public class CustomSplitCharacters extends DefaultSplitCharacters {

    @Override
    public boolean isSplitCharacter(GlyphLine text, int glyphPos) {

        if (super.isSplitCharacter(text, glyphPos)) return true;

        int charCode = text.get(glyphPos).getUnicode();

        return (charCode == '_' || charCode == '&' || charCode == '?' ||
                charCode == '='); 
    }
}

Note that I've added some extra split characters: "&", "?" and "=".

The second step is to use the HtmlConverter to generate the pdf. The code is as follow:

    File dest = new File("C:/Temp/target_pdf_file.pdf");

    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    pdf.setDefaultPageSize(PageSize.A4);

    ConverterProperties converterProperties = new ConverterProperties();
    FontProvider dfp = new DefaultFontProvider(true, true, true);
    converterProperties.setFontProvider(dfp);
    converterProperties.setImmediateFlush(false);

    Document doc = HtmlConverter.convertToDocument(htmlCode, pdf , converterProperties);
    doc.setProperty(Property.SPLIT_CHARACTERS, new CustomSplitCharacters());
    doc.relayout();

    doc.close();
tnas
  • 510
  • 5
  • 16