• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Export two page pdfs from style

Advisor ,
Jun 28, 2017 Jun 28, 2017

Copy link to clipboard

Copied

Can someone please help with the script below??

What I have is a multi page InDesign doc with the a paragraph style applied on the odd # pages, the style is used to rename the pdfs on export.

it exports pages 1-2, 3-4, 5-6 7-8, 9-10, and so on...............as separate two page pdfs twice, each named slightly different one for the hi res and one for the low res output to specific folders within a project. It works fine if the style is applied to the odd pages.

My question/problem is if the paragraph style is applied to the even pages the script errors. Is it even possible to export the pdfs named by the style if its applied to the even pages?

Thanks in advance!

error.png

   var doc = app.activeDocument;

   

   for(var p = 0; p < app.documents[0].pages.length; p=p+2) { 

     var frames = app.documents[0].pages

.textFrames; 

     var pdf_name = null; 

 

     for(var i = 0; i < frames.length; i++) { 

          if(frames.paragraphs[0].appliedParagraphStyle.name == 'UAVC-IHD') { 

               pdf_name = frames.paragraphs[0].contents; 

               break; 

          } 

     } 

     if(pdf_name != null) { 

     app.pdfExportPreferences.pageRange =

     "+"+( doc.pages

.documentOffset+1 ) + "," +

     "+"+( doc.pages[p+1].documentOffset+1 );

 

           var export_preset1 = app.pdfExportPresets.item("[High Quality Print]");

          var export_preset2 = app.pdfExportPresets.item("[Smallest File Size]");

 

var myFolder = app.activeDocument.filePath.parent;

var myDoc = app.activeDocument;

}   

 

myDoc.exportFile(ExportFormat.PDF_TYPE, File(myFolder+'/Final_Deliverables/Publish/' + pdf_name + "HR.pdf"), false, export_preset1);

myDoc.exportFile(ExportFormat.PDF_TYPE, File(myFolder+'/Final_Deliverables/Proofs/'  + pdf_name + "-00.pdf"), false, export_preset2);

     } 

alert("Done Exporting Pdf's!");

TOPICS
Scripting

Views

1.4K

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
community guidelines

correct answers 1 Correct answer

Mentor , Jul 04, 2017 Jul 04, 2017

Hi,

Right, line #17 should take page's name:

app.pdfExportPreferences.pageRange = myDoc.pages[p-1].name + "-" + myDoc.pages[p].name;

Jarek

Votes

Translate

Translate
Community Expert ,
Jun 29, 2017 Jun 29, 2017

Copy link to clipboard

Copied

Hi Mike,

you start your for loop that is running through the pages with 0, that's the index of the first page in your document.

An odd page perhaps and not an even page.

Simply start your for loop with 1, that's the index of the second page in your document.

Hopefully a even page.

Change your for loop to:

var p = 1;

Now, you want export pairs of pages.

Do you want to export an even page with the accompanied odd page on the same spread?

That question should be answered before doing any other changes to your script.

If the answer is yes you need two conditions with your document:

1. The documenta always starts with an odd page

2. The document always ends with an odd page

Regards,
Uwe

Votes

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
community guidelines
Advisor ,
Jun 29, 2017 Jun 29, 2017

Copy link to clipboard

Copied

All,

The answer to Uwu's question is yes. I want to export an even page with the accompanied odd page but the document is not setup as facing pages/spreads, its setup as single consecutive pages starting with an odd page and ending with a even page and the desired pdf export is separate two page pdfs of every consecutive two pages named by the style applied to the even pages.

Thanks in advance!

M

Votes

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
community guidelines
Community Expert ,
Jun 29, 2017 Jun 29, 2017

Copy link to clipboard

Copied

Hi Mike,

see my reply #3 :

What do you like to do with that last even page?

A, B or C ( or something else ) ?

Currently you do a schema like that:

// Let's simulate a 6 pages document.

// Run this in the ESTK. No InDesign necessary.

var pageLength = 6; // used in stop condition, corresponding to your page length

var startAt = 1; // Index based on 0, corresponding to page 2 of your document

// The next two vars have something in common.

// But depending on your needs they need not to.

var step = 2; // places you want to visit in the loop

var outputLengthEveryTime = 2 ;

// Used as a simple counter that counts loops:

var c = 0;

for( var p = startAt ; p < pageLength ; p = p + step )

{

    $.writeln( c+"\t"+"Document Page "+( p+1 ) +" - "+ "Document Page "+( p+outputLengthEveryTime ) );

    // Count up one time every loop

    c++

};

/*

Output:

0    Document Page 2 - Document Page 3

1    Document Page 4 - Document Page 5

2    Document Page 6 - Document Page 7

Page 7 is not there so InDesign will throw an error in the last loop.

*/

If B, you could check at the beginning of every loop :

if( p + outputLengthEveryTime > pageLength )

and react upon that.

Regards,
Uwe

Votes

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
community guidelines
Advisor ,
Jun 30, 2017 Jun 30, 2017

Copy link to clipboard

Copied

Hi Uwe!

To answer the question  "What do you like to do with that last even page?"

A, B or C ( or something else ) ?

C or something else........... I want to export the last even page with the last odd page (the page before).

same as the scenario below and what the code is doing with the style on the odd page, the document will alway have an even number of pages to be paired up with one another. When/if the style for the pdf naming is on the even page it errors.

export pages 1-2, 3-4, 5-6 7-8, 9-10, and so on...............as separate two page pdfs

Thanks for any input!

M

Votes

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
community guidelines
Community Expert ,
Jun 30, 2017 Jun 30, 2017

Copy link to clipboard

Copied

Hi Mike,

you might not see it but the answer is:
Include an if statement as first line in the loop that loops the pages:

if( p+2 > app.documents[0].pages.length ){ break };

or check in advance if the number of pages is sufficient and reduce the stop point of the loop if necessary.

Then it would work like that:

var startAt = 1; // Index based on 0, corresponding to page 2 of your document

var pageLength = 6; // This time NOT used in stop condition.

// The next two vars have something in common.

// But depending on your needs they need not to.

var step = 2;

var outputLengthEveryTime = 2 ;

var stopAt = parseInt( ( pageLength - startAt ) / outputLengthEveryTime ) * outputLengthEveryTime ;

var c = 0;

for( var p = startAt ; p < stopAt ; p = p + step )

{

    $.writeln( c+"\t"+"Document Page "+[ p+1 ] +" - "+ "Document Page "+[ p+outputLengthEveryTime ] );

    // Count up one time every loop

    c++

};

The output in the JavaScript console will be:

0    Document Page 2 - Document Page 3

1    Document Page 4 - Document Page 5

Test that with different values for startAt, pageLength step and outputLengthEveryTime .

With your variables and code your stopAt will be:

var stopAt = parseInt( ( app.documents[0].pages.length - 1 ) / 2 ) * 2;

Where 1 means the start position of the loop and 2 is the number of pages you want to export every time without overlap of pages.

After setting stopAt the first line of your loop will read:

for(var p = 1; p < stopAt; p=p+2) {

Regards,
Uwe

Votes

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
community guidelines
Advisor ,
Jul 01, 2017 Jul 01, 2017

Copy link to clipboard

Copied

Hi Uwe!

I went the route of adding the if statement as first line in the loop that loops the pages "if( p+2 > app.documents[0].pages.length ){ break };"

I changed the for loop to var p = 1, and the document Offset+0.

It's working where its exporting separate two page pdfs 1-2, 3-4, 5-6 7-8, 9-10.............. named by the paragraph style applied the even pages. I'm just not getting the pdf of the last set of odd and even pages on export.

Thanks

M

Votes

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
community guidelines
Community Expert ,
Jul 01, 2017 Jul 01, 2017

Copy link to clipboard

Copied

I thought you want to export only even to odd pages with every export.
And if there is no odd page next to an even page you want to drop that…

Ok. If you want to get the last even page you have to do the export of exactly that page before doing the break.

Regards,
Uwe

Votes

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
community guidelines
Advisor ,
Jul 02, 2017 Jul 02, 2017

Copy link to clipboard

Copied

Hi Uwe!

Sorry, I'm not sure if I am explaining this correctly or understanding you.

These are single consecutive pages starting with an odd page and ending with a even page and the desired pdf export is separate two page pdfs of every consecutive two pages named by the style applied to the even pages.

As it is right now if I have a ten page document the script only exports separate two page pdfs of 1-2, 3-4, 5-6 7-8 and not 9-10.

If I add the next odd page, page 11 to the document then pages 9-10 export together along with the others.

You stated "If you want to get the last even page you have to do the export of exactly that page before doing the break."

In this case that would be Page 10 but I want pages 9-10 to export together as a two page pdf.

What would be the best solution to get the the last two pages to export together??

Thanks for you help!

M

Votes

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
community guidelines
Community Expert ,
Jul 03, 2017 Jul 03, 2017

Copy link to clipboard

Copied

Hi Mike,

start your loop with 0 .
Then everything should work.

Regards,
Uwe

Votes

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
community guidelines
Advisor ,
Jul 04, 2017 Jul 04, 2017

Copy link to clipboard

Copied

I am Back to getting the same error!

Votes

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
community guidelines
Advisor ,
Jul 04, 2017 Jul 04, 2017

Copy link to clipboard

Copied

   This is the code thats working except for exporting the last set.

var doc = app.activeDocument;

   

   for(var p = 1; p < app.documents[0].pages.length; p=p+2) {

     if( p+2 > app.documents[0].pages.length ){ break};

     var frames = app.documents[0].pages

.textFrames; 

     var pdf_name = null; 

 

     for(var i = 0; i < frames.length; i++) { 

          if(frames.paragraphs[0].appliedParagraphStyle.name == 'UAVC-IHD') { 

               pdf_name = frames.paragraphs[0].contents; 

               break; 

          } 

     } 

     if(pdf_name != null) { 

     app.pdfExportPreferences.pageRange =

     "+"+( doc.pages

.documentOffset+0 ) + "," +

     "+"+( doc.pages[p+1].documentOffset+0 );

 

           var export_preset1 = app.pdfExportPresets.item("[High Quality Print]");

           var export_preset2 = app.pdfExportPresets.item("[Smallest File Size]");

 

var myFolder = app.activeDocument.filePath.parent;

var myDoc = app.activeDocument;

}   

myDoc.exportFile(ExportFormat.PDF_TYPE, File(myFolder+'/Final_Deliverables/Publish/' + pdf_name + "HR.pdf"), false, export_preset1);

myDoc.exportFile(ExportFormat.PDF_TYPE, File(myFolder+'/Final_Deliverables/Proofs/'  + pdf_name + "-00.pdf"), false, export_preset2);

    }

 

alert("Done Exporting Pdf's!");

Votes

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
community guidelines
Mentor ,
Jul 04, 2017 Jul 04, 2017

Copy link to clipboard

Copied

Hi,

Run this:

var

    myDoc = app.activeDocument,

    p, i, frames, pdf_name,

    export_preset1 = app.pdfExportPresets.item("[High Quality Print]"),

    export_preset2 = app.pdfExportPresets.item("[Smallest File Size]"),

    myFolder = myDoc.filePath.parent;

for(p = 1; p < myDoc.pages.length; p=p+2) {

    frames = myDoc.pages

.textFrames;

    pdf_name = null;

    for(i = 0; i < frames.length; i++) {

        if(frames.paragraphs[0].appliedParagraphStyle.name == 'UAVC-IHD') {

           pdf_name = frames.paragraphs[0].contents;

           break;

           }

        }

    if(!pdf_name) continue;

    app.pdfExportPreferences.pageRange = p-1 + "-" + p;

    myDoc.exportFile(ExportFormat.PDF_TYPE, File(myFolder+'/Final_Deliverables/Publish/' + pdf_name + "HR.pdf"), false, export_preset1);

    myDoc.exportFile(ExportFormat.PDF_TYPE, File(myFolder+'/Final_Deliverables/Proofs/'  + pdf_name + "-00.pdf"), false, export_preset2);

    }

alert("Done Exporting Pdf's!");

Is it work?

Jarek

Votes

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
community guidelines
Advisor ,
Jul 04, 2017 Jul 04, 2017

Copy link to clipboard

Copied

Hi Jarek!

I am getting the correct number of files to output, but the first file contains all the pages of the document and the rest are

even /odd page combos.......2-3, 4-5, 6-7, the desired output is odd /even page combos  1-2, 3-4, 5-6, 7-8........

Thanks for your reply!

M

Votes

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
community guidelines
Mentor ,
Jul 04, 2017 Jul 04, 2017

Copy link to clipboard

Copied

Hi,

Right, line #17 should take page's name:

app.pdfExportPreferences.pageRange = myDoc.pages[p-1].name + "-" + myDoc.pages[p].name;

Jarek

Votes

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
community guidelines
Advisor ,
Jul 04, 2017 Jul 04, 2017

Copy link to clipboard

Copied

Thank you Jarek!!!!

It's working now as it was intended!

Votes

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
community guidelines
Community Expert ,
Jul 05, 2017 Jul 05, 2017

Copy link to clipboard

Copied

Hi Jarek,

thank you for chiming in.

Was too busy looking into it.

Plus nearly no chance getting online.

About your used line of code:

FWIW: I'd prefer always to use the absolute numbering scheme.

You never know if there are sections with section prefixing that could get you into trouble.

Or if page names are unique.

A pageRange like that: "1-2" could be ambigous.

A string like that "+1-+2" or "+1,+2" would always denote a range of unique pages in a document.

And it could be easily derived from a schema like that:

"+" + ( page.documentOffset + 1 )

Thanks,
Uwe

Votes

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
community guidelines
Mentor ,
Jul 05, 2017 Jul 05, 2017

Copy link to clipboard

Copied

LATEST

Thanks for clarify this, Uwe.

Jarek

Votes

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
community guidelines
Mentor ,
Jun 29, 2017 Jun 29, 2017

Copy link to clipboard

Copied

Hi,

1. Open your script from ESTK (ExtendScript Toolkit)

2. Place a Breakpoint clicking like in the picture:

jsx.png

3. Run script (make sure is targeted to InDesign)

4. Watch variable values in Data Browser Panel (Cntl + Alt + F to show)

5. Is any "undefined" assigned?

Jarek

Votes

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
community guidelines
Community Expert ,
Jun 29, 2017 Jun 29, 2017

Copy link to clipboard

Copied

Hi Jarek,

I think, Mike's script likes to address a page that is not existing at output with his pageRange statement.

If the last page in the document is an even page the script tries to export the next page as well. But there is no next page. So one part of the pageRange is undefined.

So let's wait what the answer to my question is: What pair of pages should be exported? Is the last page in the document an even or an odd page? In case of an even page we need a condition what the script should do in this case.


A. Do nothing with the even page?

B. Export the even page only?

C. Add a page to the document and export the even with the new page added?

??? etc.pp.

Regards,

Uwe

Votes

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
community guidelines
Guide ,
Jun 29, 2017 Jun 29, 2017

Copy link to clipboard

Copied

For the last page issue, we can use try.. catch.. method to avoid error prompt I hope

Votes

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
community guidelines
Mentor ,
Jun 29, 2017 Jun 29, 2017

Copy link to clipboard

Copied

Hi Uwe,

You're probably right

Jarek

Votes

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
community guidelines