cancel
Showing results for 
Search instead for 
Did you mean: 

alfresco - convert docx to pdf and create a new version

spyridon_xantho
Champ in-the-making
Champ in-the-making

I' m trying to convert a docx document to pdf and store the newly created pdf file as a new version. This is the test code:

var document = search.findNode("workspace://SpacesStore/30f334f3-d357-4ea6-a09f-09eab2da7488"); 

var folder = document.parent 

var pdf = document.transformDocument('application/pdf');


document.name = "new-" + document.name + ".pdf"; d

ocument.mimetype = "application/pdf";

document.content = pdf.content;

document.save();

The document ends up empty. Is this type of conversion possible with javaScript?

2 REPLIES 2

douglascrp
World-Class Innovator
World-Class Innovator

I don't think you will be able to use JavaScript for what you want to achieve.

Check this project's source code. In it you will find lots of samples for content related operations, but using Java.

alfresco-pdf-toolkit/pdf-toolkit-repo/src/main/java/org/alfresco/extension/pdftoolkit/repo/action/ex... 

mehe
Elite Collaborator
Elite Collaborator

I think the main problem is that you use the content property. Using the properties.content.write method should work

var pdfNode=document.transformDocument("application/pdf");
var version=document.createVersion("new version pdf",true); //true=create major version
document.properties.content.write(pdfNode.properties.content,true,false);
document.name+=".pdf";
document.save();
pdfNode.remove();
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

1: create the pdf Version of the file

2: we create a new major version (you could also create a minor using false) and automatically add the versioned aspect if needed.

3: copies the content from pdf to new version, applymimetype=true, guessencoding=false 

docs:

write | Alfresco Documentation 

createVersion | Alfresco Documentation