0

I am forming a pdf file. When generating a file locally in Idea - pdf is generated correctly. And if you send this file to MiniO S3 - instead of Russian letters, the symbols '#' are generated

I myself tried to specify the encoding explicitly via metadata.setContentType ("application / pdf; charset = utf-8"); Does not help :-(

Now I'm more inclined to add fonts. Tell me how I can add this to the existing code.

Thank you in advance!

@SneakyThrows
    public byte[] createDocument(PaymentInstructionModel model) {
        WordprocessingMLPackage word = Docx4J.load(new ClassPathResource("template.docx").getInputStream());
        MainDocumentPart mainDocumentPart = word.getMainDocumentPart();
        Map<String, String> variables = objectMapper.convertValue(model, new TypeReference<>() {});
        mainDocumentPart.variableReplace(variables);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        Docx4J.toPDF(word, os);
        return os.toByteArray();
    }
        byte[] document = documentService.createDocument(model);
        String key = String.format("%s/%d-%d-%d_Платёж_№%s.pdf",
                event.getPaymentNumber(),
                event.getPaymentDate().getYear(),
                event.getPaymentDate().getMonthValue(),
                event.getPaymentDate().getDayOfMonth(),
                event.getPaymentNumber());

        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(document.length);

        amazonS3.putObject(S3Buckets.CLIENT_PAYMENT_PDF_BUCKET, key, new ByteArrayInputStream(document), metadata); 

  • May be using the library IText? https://mvnrepository.com/artifact/com.itextpdf/itextpdf – Leila Boord Jan 15 '21 at 11:48
  • Your code does not appear to be using iText. – Amedee Van Gasse Jan 15 '21 at 14:04
  • ''' byte[] docBytes = documentService.createDocument(model); Document document = new Document(); BaseFont helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED); Font font = new Font(helvetica, 12, Font.NORMAL); PdfWriter.getInstance(document, new FileOutputStream("test.pdf")); document.open(); document.add(new Chunk(Arrays.toString(docBytes), font)); // TODO document.close(); ''' – Leila Boord Jan 15 '21 at 14:15

1 Answers1

0

try this method:

public String escapeHtml(String value) {
    if (value == null) {
        return "";
    } else {
        return value
                .replaceAll("\u001F", "")
                .replaceAll("&", "&amp;")
                .replaceAll("<", "&lt;")
                .replaceAll(">", "&gt;")
                .replaceAll("\"", "&quot;");
    }
}
Hard_Coder
  • 718
  • 6
  • 13