Hello @kirill.novinskiy
let me share the piece of code where we used to convert the documents into pdf. kindly refer and update
package com.nissan.ask2.aspose.core.operations;
import com.nissan.ask2.aspose.core.services.AsposeService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.nuxeo.ecm.automation.core.Constants;
import org.nuxeo.ecm.automation.core.annotations.Context;
import org.nuxeo.ecm.automation.core.annotations.Operation;
import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
import org.nuxeo.ecm.automation.core.annotations.Param;
import org.nuxeo.ecm.automation.core.util.Properties;
import org.nuxeo.ecm.core.api.*;
import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder;
import org.nuxeo.ecm.platform.audit.api.AuditLogger;
import org.nuxeo.ecm.platform.audit.api.LogEntry;
import org.nuxeo.runtime.api.Framework;
import java.io.Serializable;
import java.util.*;
import static com.nissan.ask2.aspose.core.Ask2Constants.*;
import static com.nissan.ask2.aspose.core.Ask2Constants.PDF_SCHEMA_CONTENT_PROPERTY;
@Operation(id = AsposeToPDFOp.ID, category = Constants.CAT_CONVERSION, label = “Convert to PDF with ASPOSE”, description = “Convert to PDF with ASPOSE (on NUXEO server).”)
public class AsposeToPDFOp {
private static final Logger LOGGER = LogManager.getLogger(AsposeToPDFOp.class);
public static final String ID = "Aspose.ToPDF";
@Context
protected AsposeService service;
@Context
protected CoreSession session;
@Param(name = "namedParameters", required = false, description = "Named parameters to pass to aspose convert options")
protected Properties namedParameters = new Properties();
@Param(name = "uidForAudit", required = false, description = "UID of document for audit (used if blob)")
protected String uidForAudit;
@OperationMethod
public Blob run(Blob blob) {
LOGGER.info("========== Aspose.ToPDF Operation Started (Blob) ==========");
LOGGER.info("Input Blob Name : {}", blob != null ? blob.getFilename() : "NULL");
LOGGER.info("Input Blob MimeType : {}", blob != null ? blob.getMimeType() : "NULL");
LOGGER.info("Input Blob Length : {}", blob != null ? blob.getLength() : "NULL");
LOGGER.info("Audit UID : {}", uidForAudit);
return convertToPDF(blob, uidForAudit);
}
@OperationMethod
public Blob run(DocumentModel document) {
LOGGER.info("========== Aspose.ToPDF Operation Started (Document) ==========");
LOGGER.info("Document Id : {}", document.getId());
LOGGER.info("Document Name : {}", document.getName());
LOGGER.info("Document Title : {}", document.getTitle());
LOGGER.info("Document Type : {}", document.getType());
LOGGER.info("Document Path : {}", document.getPathAsString());
Blob sourceBlob = (Blob) document.getPropertyValue(FILE_SCHEMA_CONTENT_PROPERTY);
if (sourceBlob == null) {
LOGGER.error("Source blob is NULL.");
return null;
}
LOGGER.info("Source Blob Filename : {}", sourceBlob.getFilename());
LOGGER.info("Source Blob MimeType : {}", sourceBlob.getMimeType());
LOGGER.info("Source Blob Length : {}", sourceBlob.getLength());
LOGGER.info("Source Blob Digest : {}", sourceBlob.getDigest());
Serializable conversionEnabled = document.getPropertyValue(PDF_SCHEMA_CONVERSION_ENABLED_PROPERTY);
LOGGER.info("PDF Conversion Enabled Property : {}", conversionEnabled);
if (conversionEnabled != null && !Boolean.parseBoolean(conversionEnabled.toString())) {
LOGGER.warn("No converter {} document, the conversion PDF is disabled.", document.getId());
return null;
}
String nativeDigest = sourceBlob.getDigest();
Serializable pdfNativeDigest = document.getPropertyValue(PDF_SCHEMA_NATIVE_DIGEST_PROPERTY);
LOGGER.info("Current Native Digest : {}", nativeDigest);
LOGGER.info("Stored PDF Digest : {}", pdfNativeDigest);
if (pdfNativeDigest != null && nativeDigest.equalsIgnoreCase(pdfNativeDigest.toString())) {
LOGGER.info("Skipping conversion because digests are identical.");
LOGGER.warn("No converter {} document with same digest {}.", document.getId(), nativeDigest);
return (Blob) document.getPropertyValue(PDF_SCHEMA_CONTENT_PROPERTY);
}
return convertToPDF(sourceBlob, document.getId());
}
private Blob convertToPDF(Blob sourceBlob, String documentId) {
LOGGER.info("----------------------------------------------------");
LOGGER.info("Entering convertToPDF()");
LOGGER.info("Document Id : {}", documentId);
if (sourceBlob != null) {
LOGGER.info("Blob Name : {}", sourceBlob.getFilename());
LOGGER.info("Blob MimeType : {}", sourceBlob.getMimeType());
LOGGER.info("Blob Length : {}", sourceBlob.getLength());
LOGGER.info("Blob Digest : {}", sourceBlob.getDigest());
}
if (documentId == null || documentId.isEmpty()) {
LOGGER.error("No documentId provided!");
}
SimpleBlobHolder blobHolder = new SimpleBlobHolder(sourceBlob);
AuditLogger auditLogger = Framework.getService(AuditLogger.class);
List<LogEntry> entries = new ArrayList<>();
LOGGER.warn("Conversion in progress for document: {}", documentId);
if (documentId != null ) {
addAuditEntry(auditLogger, entries, documentId, "Conversion in progress");
}
try {
Map<String, Serializable> parameters = new HashMap<>(namedParameters);
LOGGER.info("Calling AsposeService.convert()");
LOGGER.info("Named Parameters : {}", parameters);
BlobHolder serviceResult = service.convert(blobHolder, parameters);
Blob resultBlob = serviceResult.getBlob();
if (resultBlob != null) {
LOGGER.info("Aspose conversion successful.");
LOGGER.info("Result Filename : {}", resultBlob.getFilename());
LOGGER.info("Result MimeType : {}", resultBlob.getMimeType());
LOGGER.info("Result Length : {}", resultBlob.getLength());
}
else {
LOGGER.warn("Aspose returned NULL Blob.");
}
if (documentId != null) {
LOGGER.warn("Conversion done for document: {}", documentId);
addAuditEntry(auditLogger, entries, documentId, "Conversion done");
}
return resultBlob;
} catch (Exception e) {
if (documentId != null) {
String errorMessage = "Conversion failed for document: " + documentId + ". Error: " + e.getMessage();
LOGGER.error(errorMessage, e);
LOGGER.error("Exception Class : {}", e.getClass().getName());
LOGGER.error("Exception Message : {}", e.getMessage());
for (StackTraceElement element : e.getStackTrace()) {
LOGGER.error(element.toString());
}
addAuditEntry(auditLogger, entries, documentId, "Conversion failed: " + e.getMessage());
LOGGER.info("Adding audit entry...");
LOGGER.info("Repository : default");
LOGGER.info("Document Id : {}", documentId);
LOGGER.info("User : {}", session.getPrincipal().getName());
}
String errorMessage = "Conversion failed : " + e.getMessage();
LOGGER.error(errorMessage, e);
return null;
}
}
private void addAuditEntry(AuditLogger auditLogger, List<LogEntry> entries,
String documentId, String message) {
LogEntry entry = auditLogger.newLogEntry();
entry.setRepositoryId("default");
entry.setEventId("documentConversion");
entry.setCategory("eventDocumentCategory");
entry.setPrincipalName(session.getPrincipal().getName());
entry.setEventDate(new Date());
entry.setDocUUID(documentId);
entry.setComment(message);
entries.add(entry);
auditLogger.addLogEntries(entries);
LOGGER.info("Audit entry successfully stored.");
LOGGER.warn("Audit log added: Document ID: {}, Event: {}", documentId, message);
}