Sie haben kein PDF -Dokument geschlossen (Thymeleaf)Java

Java-Forum
Anonymous
 Sie haben kein PDF -Dokument geschlossen (Thymeleaf)

Post by Anonymous »

Ich versuche, eine PDF zu generieren, und meine Methode gibt PDFStream -Daten zurück. Hier ist meine Methode. < /P>
private byte[] generateDocument(String language,String serviceType,String documentType) throws Exception {
// Construct the template path based on document type, service type, and language.
String templatePath = documentType + "/" + serviceType + "/" + language+".html";
// Check if the template exists.
if (templateCheckerService.templateExists(templatePath)) {
try{
// Prepare data for the template.
Map data = new HashMap();
data.put("requestParameter", requestParameter);

// Create Thymeleaf template engine
TemplateEngine templateEngine = new TemplateEngine();
Context context = new Context();
context.setVariables(data);

// Load and add images as Base64 strings to the context.
context.setVariable("logo", loadImageAsBase64("classpath:static/images/logo.png"));
context.setVariable("cLogo", loadImageAsBase64("classpath:static/images/con.png"));

// Process the template with the provided data.
String htmlContent = templateEngine.process(loadHtmlTemplate("classpath:templates/" + templatePath), context);

// Convert HTML to PDF using Flying Saucer library.
ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.useFastMode();

// Load the font from the classpath.
try (InputStream fontStream = resourceLoader.getResource("classpath:static/fonts/NotoSansMonoCJKjp-VF.ttf").getInputStream()) { // Try-with-resources for font stream
builder.useFont(new FSSupplier() {
@Override
public InputStream supply() {
return fontStream;
}
}, "NotoSansMonoCJKjp-VF");

Thread.sleep(500); // Wait for 500 milliseconds (might be needed for font loading).
builder.withHtmlContent(htmlContent, null); // Set the HTML content for PDF generation.
builder.toStream(pdfOutputStream); // Set the output stream for the PDF.
builder.run(); // Generate the PDF.
pdfOutputStream.close();
} // Font stream will be closed automatically here
// Return the generated PDF as a byte array.
return pdfOutputStream.toByteArray();
}catch (Exception ex){
// Handle exceptions during document generation.
throw new Exception(ex);
}
}else {
// Throw an exception if the template doesn't exist.
throw new RuntimeException("Template doesn't exist");
}
}
< /code>
Meine Implementierung generiert perfekt PDF- und Return Base64 -Antwort. Schließen Sie kein PDF -Dokument
Wie kann ich diesen Fehler beheben? Aufgrund des Fehlers habe meine API-Rückgabe "Handler-Versand fehlgeschlagen: java.lang.outofMemoryError: Java Heap Space"
Ich habe es mit Verwendung von Try-with-Ressourcen versucht, wenn möglich besonders für ** bytearrayoutputStream ** und InputStream . Zusätzlich pdfrendererBuilder funktioniert aber nicht.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post