Folder Level Javascripts Not Working - Save To Txt

New Here ,
Dec 05, 2019

Copy link to clipboard

Copied

Hello. We are using folder level javascripts to save data from our PDF files to a text file for data imports. This worked very well on windows but now were trying to use the same system on a Mac Mojave 10.14.6 machine.

 

Currently it is creating the text file but it is not writing the data to the file. In this situation, the variable for our data is "lines". We ensured that the data is coming from lines by making its value ".txt" and adding it to the path.

 

Here is the code:

 

qipSaveCSV = app.trustedFunction(function(doc, path, lines) {
	app.beginPriv(); 
	myDoc = app.newDoc();
	myDoc.saveAs(path, "com.adobe.acrobat.plain-text","", false, true);
	var f = myDoc.addField("lines", "text", 0, [0, 0, 0, 0]);
	f.multiline = true;
	f.value = lines;	
	myDoc.saveAs(path, "com.adobe.acrobat.plain-text", "", false, true);
	myDoc.closeDoc(true);
	app.endPriv(); 
});

 

 If you have any ideas as to why this isn't functioning properly, help would be greatly appreciated. 

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

You need to move the saveAs command just before the closeDoc command. What you're doing now is saving the file before adding the text to it... Of course that will yield an empty text file.

TOPICS
Acrobat SDK and JavaScript, Edit and convert PDFs, General troubleshooting, How to, PDF forms

Views

153

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Folder Level Javascripts Not Working - Save To Txt

New Here ,
Dec 05, 2019

Copy link to clipboard

Copied

Hello. We are using folder level javascripts to save data from our PDF files to a text file for data imports. This worked very well on windows but now were trying to use the same system on a Mac Mojave 10.14.6 machine.

 

Currently it is creating the text file but it is not writing the data to the file. In this situation, the variable for our data is "lines". We ensured that the data is coming from lines by making its value ".txt" and adding it to the path.

 

Here is the code:

 

qipSaveCSV = app.trustedFunction(function(doc, path, lines) {
	app.beginPriv(); 
	myDoc = app.newDoc();
	myDoc.saveAs(path, "com.adobe.acrobat.plain-text","", false, true);
	var f = myDoc.addField("lines", "text", 0, [0, 0, 0, 0]);
	f.multiline = true;
	f.value = lines;	
	myDoc.saveAs(path, "com.adobe.acrobat.plain-text", "", false, true);
	myDoc.closeDoc(true);
	app.endPriv(); 
});

 

 If you have any ideas as to why this isn't functioning properly, help would be greatly appreciated. 

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

You need to move the saveAs command just before the closeDoc command. What you're doing now is saving the file before adding the text to it... Of course that will yield an empty text file.

TOPICS
Acrobat SDK and JavaScript, Edit and convert PDFs, General troubleshooting, How to, PDF forms

Views

154

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 05, 2019 0
Adobe Community Professional ,
Dec 05, 2019

Copy link to clipboard

Copied

So first, are there any errors reported in the Console Window?

Next, you don't need the two SaveAs operations, one is just fine.

And finally, this code is creating a blank document, with a text field of zero size. There is no reason for the data to appear in the final document. The text field should be of a decent size. Like page size.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 05, 2019 0
New Here ,
Dec 05, 2019

Copy link to clipboard

Copied

No Errors in the Console Window.  I removed one of the SaveAs and it still just creates a blank file.  We tried the code below but still got a blank file.  Any suggestions?

 

var f = myDoc.addField("lines", "text",0 , [20, 100, 100, 20]);

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 05, 2019 0
Adobe Community Professional ,
Dec 05, 2019

Copy link to clipboard

Copied

Remove the line that closes the file, so you can see the result. Does the "lines" text appear in the text field?

If not, then perhaps there is a missing conversion setting for text files. One that includes form fields in the conversion. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 05, 2019 0
New Here ,
Dec 05, 2019

Copy link to clipboard

Copied

The data is showing up in the field and we have been able to pass the data previously as a file extension.

I'm afraid this is some weird Mac compatability issue.

Do you have any other ideas?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 05, 2019 0
Adobe Community Professional ,
Dec 05, 2019

Copy link to clipboard

Copied

Try this:

Add myDoc.flattenPages ();

before saveAs

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 05, 2019 0
New Here ,
Dec 05, 2019

Copy link to clipboard

Copied

Hey Bernd.

 

I tried that, and this is the code as of now, with no luck.

qipSaveCSV = app.trustedFunction(function(doc, path, lines) {
	
	app.beginPriv(); 
	myDoc = app.newDoc();
	myDoc.flattenPages ();
	myDoc.saveAs(path, "com.adobe.acrobat.plain-text","", false, true);
	var f = myDoc.addField("lines", "text",0,[20, 100, 100, 20]);
	f.multiline = true;
	f.value = lines;	
	
	
	myDoc.closeDoc(true);
	app.endPriv();

});

 

Any other suggestions?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 05, 2019 0
Most Valuable Participant ,
Dec 05, 2019

Copy link to clipboard

Copied

You need to move the saveAs command just before the closeDoc command. What you're doing now is saving the file before adding the text to it... Of course that will yield an empty text file.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 05, 2019 1
Adobe Community Professional ,
Dec 05, 2019

Copy link to clipboard

Copied

Move flattenPages and saveAs before closeDoc.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 05, 2019 0
New Here ,
Dec 05, 2019

Copy link to clipboard

Copied

Flatten pages was giving me an error so I removed it after moving save as to before close Doc.

 

It is officially working now, and this is the code:

 

qipSaveCSV = app.trustedFunction(function(doc, path, lines) {
	app.beginPriv(); 
	myDoc = app.newDoc();
	var f = myDoc.addField("lines", "text",0,[20, 100, 100, 20]);
	f.multiline = true;
	f.value = lines;	
	myDoc.saveAs(path, "com.adobe.acrobat.plain-text","", false, true);
	myDoc.closeDoc(true);
	app.endPriv();

});

Thanks everyone for all of the support! This stump has been slowing us up a bit!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 05, 2019 0
Most Valuable Participant ,
Dec 05, 2019

Copy link to clipboard

Copied

Don't use a variable called "path". There's a built-in property of the Doc object with that name and it can cause conflicts.

Change it to something else.

Also, why are you saving the file twice as a text file?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 05, 2019 0
New Here ,
Dec 05, 2019

Copy link to clipboard

Copied

Very good insight, let me swap that out.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 05, 2019 0