Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

java - PdfBoxRenderer with custom width and height

I am having html content store as a raw string in my database and I like to print it in pdf, but with custom size, for example page size to be 10cm width and 7 com height, not standard A4 format.

Can someone gives me some examples if it is possible.

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PDRectangle rec = new PDRectangle(recWidth, recHeight);
    PDPage page = new PDPage(rec);

    try (PDDocument document = new PDDocument()) {
        PdfRendererBuilder builder = new PdfRendererBuilder();
        builder.defaultTextDirection(BaseRendererBuilder.TextDirection.LTR);
        String htmlContent = "<b>Hello world</b>" + content; 
        builder.withHtmlContent(htmlContent, "");

        document.addPage(page);
        builder.usePDDocument(document);
        PdfBoxRenderer renderer = builder.buildPdfRenderer();
        renderer.createPDFWithoutClosing();

        document.save(out);
    } catch (Exception e) {
      ex.printStackTrace();
    }
    return new ByteArrayInputStream(out.toByteArray());

This code generates for me 2 files, one small and one A4.

UPDATE:

I tried this one:

  try (PDDocument document = new PDDocument()) {
        PdfRendererBuilder builder = new PdfRendererBuilder();
        builder.defaultTextDirection(BaseRendererBuilder.TextDirection.LTR);
        builder.useDefaultPageSize(210, 297, PdfRendererBuilder.PageSizeUnits.MM);
        builder.usePdfAConformance(PdfRendererBuilder.PdfAConformance.PDFA_3_A);
        String htmlContent = "<b>content</b>";
        builder.withHtmlContent(htmlContent, "");
        builder.usePDDocument(document);
        PdfBoxRenderer renderer = builder.buildPdfRenderer();
        renderer.createPDFWithoutClosing();
        document.save(out);
    } catch (Exception e) {
        log.error(">>> The creation of PDF is invalid!");
    }
       

But in this case content is not shown, if I remove useDefaultPageSize, content will be shown

question from:https://stackoverflow.com/questions/65866039/pdfboxrenderer-with-custom-width-and-height

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I didn't check this solution before, but try initialise the builder object with your desired page size and document type like below

    builder.useDefaultPageSize(210, 297, PdfRendererBuilder.PageSizeUnits.MM);
    builder.usePdfAConformance(PdfRendererBuilder.PdfAConformance.PDFA_3_A);

the lib include many PDF format next is PdfAConformance Enum with possible values

PdfAConformance Enum


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...