I use ITextRenderer to create PDF from HTML, and I need to get a cash register receipt. This check has dynamic width and, of course, dynamic content. At the same time, the height of the content will always be different, and now I'm struggling to find a way to fit the height of the PDF page to the content. If the receipt is too large, at the end there is a long white section, and if it is shortened, the PDF is paginated, and I need it to be on only one page.
I use @page {size: Wpx Hpx;} to set the page size, but it is almost impossible (it would be very painful) to calculate the height of the content based on the width and data.
This is the code that the PDF generates:
ITextRenderer renderer = new ITextRenderer(); byte[] bytes = htmlDocumentString.toString().getBytes("UTF-8"); ByteArrayInputStream bais = new ByteArrayInputStream(bytes); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource is = new InputSource(bais); Document doc = builder.parse(is); renderer.setDocument(doc, null); renderer.layout(); renderer.createPDF(outputStream); outputStream.flush(); outputStream.close();
I also tried renderer.getSharedContext().setPrint(false); but it throws NPE.
Also @page {-fs-page-sequence: "none";} without any luck.
source share