Lambda Spring boot converting html to pdf
Asked Answered
I

0

0

can you suggest a solution so that the classpath find the logo in resources and the fonts?

@Component
public class LambdaHandler implements Consumer<Map<String, Object>> {

    private final HtmlGenerationService htmlGenerationService;
    private final UploadFileService uploadFileService;
    private final ExtractDataService extractDataService;

    @Autowired
    public LambdaHandler(HtmlGenerationService htmlGenerationService, UploadFileService uploadFileService, ExtractDataService extractDataService) {
        this.htmlGenerationService = htmlGenerationService;
        this.uploadFileService = uploadFileService;
        this.extractDataService = extractDataService;
    }

    @Override
    public void accept(Map<String, Object> notification) {

        LOGGER.info("Received message: {}", notification);

        @SuppressWarnings("unchecked")
        var records = (List<Map<String, Object>>) notification.get("Records");
        records
                .stream()
                .map(record -> (String) record.get("body"))
                .forEach(this::handleRequest);
    }

    public void handleRequest(String sqsMessageBody) {



      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

      var data = extractDataService.extractCompensationData(sqsMessage);
      String html = htmlGenerationService.generateCompensationHtml(data);
      HtmlConverter.convertToPdf(html, byteArrayOutputStream, new ConverterProperties());
      uploadFileService.uploadPdfToS3(byteArrayOutputStream, data.getFileName(), contractId, pdvId)
    }
public void uploadPdfToS3(ByteArrayOutputStream pdfData, String pdfFileName, String contractId, String pdvId) {
       

        metadata.setContentLength(pdfData.toByteArray().length);
        s3.putObject(new PutObjectRequest(s3Properties.getPrivateBucket(), PATH_BUCKET + pdfFileName,
        new ByteArrayInputStream(pdfData.toByteArray()), metadata));
        
    }
public String generateCompensationHtml(List<Data> data) {


        Context context = new Context();
        context.setVariable("data", data);  

        return templateEngine.process("invoice_template", context);
    }
<head>

    <meta charset="UTF-8"/>

    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>

    <title>Invoice</title>

    <style>
        @font-face {
            font-family: 'VerdanaFont';
            src: url('classpath:static/verdana.ttf');
        }

        @font-face {
            font-family: 'VerdanaFontBold';
            src: url('classpath:static/verdana-bold.ttf');
        }

</style>

<header class="invoice-header">

    <div class="header-block">

        <img  src="classpath:static/logo.png" style="width: 35%"/>

    </div>

    <div class="header-block">

        <strong style="font-family: VerdanaFontBold, sans-serif;">FACTURE</strong>

        <p style="margin-top: 0;">To be paid by the company</p>

            Numéro facture : <span th:text="${data.getInvoiceNumber()}"></span><br/>

    </div>

</header>

in the code I sent it's a lambda that receives an sqs event with data with which it will create html then convert it to pdf to upload it to S3 the problem is that I have these errors

2023-10-24T21:01:55.109+02:00

Copier
2023-10-24T19:01:55.108Z ERROR 8 --- [           main] c.i.s.r.resource.ResourceResolver        : Unable to retrieve stream with given base URI (file:/var/task/) and source path (classpath:static/verdana.ttf)
2023-10-24T19:01:55.108Z ERROR 8 --- [ main] c.i.s.r.resource.ResourceResolver : Unable to retrieve stream with given base URI (file:/var/task/) and source path (classpath:static/verdana.ttf)

2023-10-24T21:01:55.109+02:00 java.net.MalformedURLException: unknown protocol: classpath
2023-10-24T21:01:57.141+02:00   2023-10-24T19:01:57.141Z ERROR 8 --- [ main] c.i.h.attach.impl.DefaultHtmlProcessor : Unable to retrieve font:

2023-10-24T21:01:57.141+02:00   @font-face {

2023-10-24T21:01:57.141+02:00   font-family: 'VerdanaFontBold';

2023-10-24T21:01:57.141+02:00   src: url('classpath:static/verdana-bold.ttf');

2023-10-24T19:01:57.144Z ERROR 8 --- [           main] c.i.h.attach.impl.DefaultHtmlProcessor   : Worker of type com.itextpdf.html2pdf.attach.impl.tags.DivTagWorker unable to process com.itextpdf.html2pdf.attach.impl.tags.ImgTagWorker

so it creates a pdf without a logo and without the font I want

Indecorum answered 24/10, 2023 at 20:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.