Script - Convert documents in a folder hierarchy to PDF
The script converts all the documents into subfolder hierarchy to PDF
The string "/okm:root/import" is the root folder.
All the documents into child folders of parent "/okm:root/import" will be processed.
The destination folder is "/okm:root/out".
import com.openkm.bean.*;
import com.openkm.api.*;
import com.openkm.bean.form.FormElement;
import com.openkm.util.*;
import com.openkm.spring.PrincipalUtils;
String dstFldPath = "/okm:root/out/";
for (Folder fld : OKMFolder.getInstance().getChilds(null, "/okm:root/import")) {
print("analize document into :" + fld.getPath() + "</br>");
File cache;
for (Document doc : OKMDocument.getInstance().getChilds(null, fld.getUuid())) {
// *** Create a pdf from an existing document
ConverterHelper.ConversionData cd = new ConverterHelper.ConversionData();
cd.uuid = doc.getUuid();
cd.mimeType = doc.getMimeType();
cd.node = doc;
cd.version = null;
long tenantId = PrincipalUtils.getTenantId();
print("tenantId: " + tenantId + "<br/>");
cache = ConverterHelper.getPdfCache(cd, tenantId);
String docPath = doc.getPath();
print("*******");
print(docPath);
print("<br/>");
print(cd);
print("<br/>");
print(cache.exists() );
print("<br/>");
if (!cache.exists()) {
try {
ConverterHelper.toPDF(cd, cache, true);
} catch (Exception e) {
String error = StackTraceUtils.toString(e);
error = error.replace("\n", "<br/>");
return (error);
}
} else {
cd.file = cache;
}
String filename = PathUtils.getName(docPath);
print("filename:" + filename + "<br/>");
filename = FileUtils.getFileName(filename);
String destinationPath = dstFldPath + filename + ".pdf";
// Create destination folder if not exists
OKMFolder.getInstance().createMissingFolders(null, dstFldPath);
InputStream is = new FileInputStream(cd.file);
OKMDocument.getInstance().createSimple(null, destinationPath, is);
is.close();
};
}
// Some value can also be returned as string
return "some result";