converting txt to pdf and rename the converted file (in the same space)

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Active Member II

converting txt to pdf and rename the converted file (in the same space)

hi everybody, may u plz help me, im not programmer and im really suffering with scripts, i need to convert a txt file into pdf then rename it or add "copy" to the name, to avoid duplication in the same space, the convertion is not the problem, my problem is to rename the file, i want to run a script that acts in this way: if the pdf file exists then the new one being created from the conversion have to be renamed but i dont know how to do it.

i know, probably is not a big issue but for me is taff, so plz help me......

12 Replies
Highlighted
Senior Member

Re: converting txt to pdf and rename the converted file (in the same space)

Hi,

The approach could be as follows:

1.- We will create a script that will be named: "convert_pdf.js" and will be located in: "Repository > Data Dictionary > Scripts" with the following content:

Var pdfNode = document.transformDocument ("application / pdf");

Var version = document.createVersion ("pdf version", true); // true = create major version

document.properties.content.write (pdfNode.properties.content, true, false);

Document.name + = ". Pdf"; Document.save ();

PdfNode.remove ();

2.- We will create a folder on which we will carry out this test, and we will denominate: "TXT to PDF"

3.- On this folder that we just created, we will define a content rule:
- When: items are created or entered in this folder
- If all the criteria are met: All elements
- Perform action: Run script - convert_pdf.js

4.- Every time we upload a document to this folder, the conversion script will be executed to PDF, and a new version of the document will be created

Maybe you can change the code to adapt it to your needs.

Regards

Highlighted
Active Member II

Re: converting txt to pdf and rename the converted file (in the same space)

hi calvo, thanks for ur feedback.

I have tried this script but is failing, is not working, i cant upload a file an error msn shows. can u fix it plz, i will try also to do it here

best regards

Highlighted
Senior Member

Re: converting txt to pdf and rename the converted file (in the same space)

Hi,

You're right. The problem was syntax error to copy in an inline editor.

The code that I've just test in Alfresco 5.2 is the following (it works):

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

Please, tell me if its ok.

Regards

Highlighted
Active Member II

Re: converting txt to pdf and rename the converted file (in the same space)

hi Calvo, thanks once again for ur feedback

im having this prob:

Failing creating content: 05080104 Failed to execute script 'workspace://SpacesStore/7d1f767c-0962-4002-a48c-b2297693305f': 05080103 Archive or folder 'testing.pdf' already exists.

Regards

Highlighted
Active Member II

Re: converting txt to pdf and rename the converted file (in the same space)

actually i saw the sintax error yesterday and changed, so that is the problem im getting i forgot to mention

Highlighted
Senior Member

Re: converting txt to pdf and rename the converted file (in the same space)

Hi,

I think the message indicates that already exists an archive o folder with name "testing.pdf". If you try to delete this file, the script should work.

Regards

Highlighted
Active Member II

Re: converting txt to pdf and rename the converted file (in the same space)

hi Calvo

actually was really giving probs, i dont know y, the file did not exist in the system but the msn getting was that one, i took a while and i´m back workin with it and for my surprise suddenly is working, mayb i have done something somewhere i dnt remember but is really workin, so thanks very much

Highlighted
Senior Member II

Re: converting txt to pdf and rename the converted file (in the same space)

Seems to be a copy of my answer to alfresco - convert docx to pdf and create a new version glad to see it still helps :-)

You could check the existence of a PDF with the same name before the transformation with 

var existingPdf=document.parent.childByNamePath(pdfName); 
if (existingPdf !== null) { existingPdf.remove()}; //if pdf is existing, delete it

‌ you can use the inline editor with syntax highlighting (under "more") to prevent you from getting errors from copying/writing lines of code. This is only available when opening the complete thread... 

Highlighted
Active Member II

Re: converting txt to pdf and rename the converted file (in the same space)

is that i was looking for, im gonna try it then will let u know, thanks very much Martin