Создание PDF, совместимый с PDF/3-A, и прикрепление счёта ZUGFeRD в Java
Contents
[
Hide
]
Используйте API Document и FileSpecification, когда вам нужно упаковать XML счёта-фактуры внутрь PDF для рабочих процессов в стиле ZUGFeRD.
Прикрепление XML-счёта ZUGFeRD к PDF
- Откройте исходный PDF в объекте Document.
- Создайте FileSpecification для XML-файла счета.
- Установите метаданные встроенного файла, включая тип MIME и AFRelationship.
- Добавьте FileSpecification в коллекцию встроенных файлов документа.
- Преобразуйте документ в PdfFormat
PDF_A_3A. - Сохраните обновлённый 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);
}