Создание PDF, совместимый с PDF/3-A, и прикрепление счёта ZUGFeRD в Java

Используйте API Document и FileSpecification, когда вам нужно упаковать XML счёта-фактуры внутрь PDF для рабочих процессов в стиле ZUGFeRD.

Прикрепление XML-счёта ZUGFeRD к PDF

  1. Откройте исходный PDF в объекте Document.
  2. Создайте FileSpecification для XML-файла счета.
  3. Установите метаданные встроенного файла, включая тип MIME и AFRelationship.
  4. Добавьте FileSpecification в коллекцию встроенных файлов документа.
  5. Преобразуйте документ в PdfFormat PDF_A_3A.
  6. Сохраните обновлённый PDF Document.
public static void attachInvoiceZugferdFormat(Path inputFile, Path invoiceFile, Path outputFile) {
        try (Document document = new Document(inputFile.toString())) {
            String description = "Invoice metadata conforming to ZUGFeRD standard";
            FileSpecification fileSpecification = new FileSpecification(invoiceFile.toString(), description);

            fileSpecification.setMIMEType("text/xml");
            fileSpecification.setAFRelationship(AFRelationship.Alternative);

            document.getEmbeddedFiles().add("factur", fileSpecification);

            String outputFileName = outputFile.toString();
            String logPath = outputFileName.replace(".pdf", "_log.xml");
            document.convert(logPath, PdfFormat.PDF_A_3A, ConvertErrorAction.Delete);
            document.save(outputFile.toString());
        }
        System.out.println("ZUGFeRD invoice attached to " + outputFile);
    }