Copy link to clipboard
Copied
Hello ASC -
I find myself under pressure to create a few thousand COVID test results per day, with great urgency, using the excel output file of a molecular biology lab that does the work and needs to have it conveyed in a special PDF form to the government.
I've solved all but the last steps of the process using DataMerge and the option to save as individual PDFs I can now generate the test bulletins. But for these tests to be distributed fast to hundreds of destinations and thousands of recipients, I need to be able to implement a naming protocol and this is where I got stuck.
Each PDF should be named using a combination of specific fields from its content, such as test_result, pacient_name, test_date, test_place, etc
I am relatively new to Indesign and have reached my competency limit with this one. I can probably crack it given enough time, but time is exactly what we lack.
Can you help me please?
Copy link to clipboard
Copied
Hello Catalin,
I will be able to help, but I have a couple of questions..........
Are each test bulletins a single page to export as a pdf? what pdf export preset do you use to export the pdfs currently.
Regards,
Mike
Copy link to clipboard
Copied
Hi Catalin,
My Extract Pages script (https://www.id-extras.com/products/extract-pages/) does just what you ask for. Not free, though.
Thanks,
Ariel
Copy link to clipboard
Copied
Hello Catalin,
1. Start off by creating a Paragraph style named "PDF_name"
2. Create a text box in the slug area with a portion of the box within the page bounds and assign the Paragraph style "PDF_name"
3. Assign the field from the data source for the pdf naming
4. Create and save the Merged document, run the script to export the pdfs
The below script will export pdfs named by the contents of the text box useing the Paragraph style named "PDF_name from each page
var doc = app.documents[0];
var myResult = {};
var allStoriesArray = app.documents[0].stories.everyItem().getElements();
for(var n=0; n<allStoriesArray.length; n++){
var currentStory = allStoriesArray[n];
var textContainersLength = currentStory.textContainers.length;
var lastFrameOfStory = currentStory.textContainers[textContainersLength-1];
var objToCheck = lastFrameOfStory;
if(lastFrameOfStory.constructor.name == "TextPath")
objToCheck = lastFrameOfStory.parent
if(currentStory.overflows && objToCheck.itemLayer.visible && objToCheck.parentPage != null)
myResult[objToCheck.parentPage.name] = objToCheck.parentPage.name;
}
var pageList = new Array;
for(var a in myResult)
pageList.push(a);
pageList.sort(function(a,b) {return a-b});
if(pageList.length > 0){
alert("Error!\nDocument contains overset text on page(s): \r" + pageList);
exit();
}
var mLinks = doc.links;
for(var i = 0; i < mLinks.length; i++){
if ((mLinks[i].status == LinkStatus.LINK_MISSING || mLinks[i].status == LinkStatus.LINK_OUT_OF_DATE) && checkIfOnPage(mLinks[i].parent.parent) && mLinks[i].parent.parent.itemLayer.visible == true ){
alert("Error!\nThis document contains links to files that are missing or modified, use the links panel to fix the problem(s) and try again!");
exit();
}
}
function checkIfOnPage(mItem){
var _OldZeroPoint = app.activeDocument.zeroPoint;
app.activeDocument.zeroPoint = [0,0];
var _Width =app.activeDocument.documentPreferences.pageWidth;
var _Height=app.activeDocument.documentPreferences.pageHeight;
var _Bounds = mItem.geometricBounds;
if ((_Bounds[3] < 0) || (_Bounds[1] > _Width) || (_Bounds[0] > _Height) || (_Bounds[2] < 0)){
return false
}else{
return true
}
app.activeDocument.zeroPoint =_OldZeroPoint;
}
function myGetPageNames(){
var myPageNames = new Array;
for(myCounter = 0; myCounter < app.activeDocument.pages.length; myCounter ++){
myPageNames.push(app.activeDocument.pages.item(myCounter).name);
}
return myPageNames;
}
//Function Returns pdf_name for Selected Pages
function getTextFromPageUsingParagraphStyle(myPage){
if (doc.paragraphStyles.itemByName("PDF_name") == null) {
alert ('Error!\nThe Paragraph Style "PDF_name" does not exist or is Labeled Incorrectly.');
exit();
}
app.findTextPreferences = app.changeTextPreferences = null;
app.findTextPreferences.appliedParagraphStyle = "PDF_name";
var myTextframes = myPage.textFrames;
var pdf_name = null;
for (y=0; y < myTextframes.length; y++){
try {
var mySearchResult = myTextframes[y].findText();
if (mySearchResult.length > 0){
pdf_name = mySearchResult[0].contents;
break;
}
}catch (e){
}
}
return pdf_name;
}
//Dialog/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var mySelectedFolder = Folder.selectDialog ("Select a Folder");
if(mySelectedFolder != null){
}else{
exit();
}
var myDialog = app.dialogs.add({name:"DataMerge 1-Page HR Pdf Exporter", canCancel:true});
with (myDialog){
with (dialogColumns.add()){
with (dialogRows.add())
staticTexts.add({staticLabel:"Page Range Options", minWidth:25});
with (borderPanels.add()){
var myradiogroup1 = radiobuttonGroups.add();
with (myradiogroup1){
var myyesradiobutton1 = radiobuttonControls.add({staticLabel:'Export All Pages', checkedState:true});
var mynoradiobutton1 = radiobuttonControls.add({staticLabel:'Export Select Pages'});
staticTexts.add({staticLabel:"", minWidth:75});
}
}
}
}
if (myDialog.show() == true){
var SelectedPages = myradiogroup1.selectedButton;
}else{
}
myDialog.destroy();
//All Pages//////////////////////////////////////////////////////////////////////////////////////////////////
if (SelectedPages == false){
for(var p = 0; p < app.documents[0].pages.length; p++) {
var frames = app.documents[0].pages[p].textFrames;
var pdf_name = null;
for(var i = 0; i < frames.length; i++) {
try {
if(frames[i].paragraphs[0].appliedParagraphStyle.name == 'PDF_name') {
pdf_name = frames[i].paragraphs[0].contents;
break;
}
}catch (e){
}
}
if (pdf_name == null) {
alert ('Error!\nThe Paragraph Style "PDF_name" is not applied to one or more of the pages or is Labeled Incorrectly.');
exit();
}
if(pdf_name != null) {
}
app.pdfExportPreferences.pageRange = app.documents[0].pages[p].name;
var export_preset = app.pdfExportPresets.item("[High Quality Print]");
doc.exportFile(ExportFormat.PDF_TYPE, File(mySelectedFolder +"/"+ pdf_name + ".pdf"), false, export_preset);
}
alert("Done Exporting Pdf's!");
}
//Selected Pages/////////////////////////////////////////////////////////////////////////////////////////////
if (SelectedPages == true){
for(var p = 0; p < app.documents[0].pages.length; p++) {
var frames = app.documents[0].pages[p].textFrames;
var pdf_name = null;
for(var i = 0; i < frames.length; i++) {
try {
if(frames[i].paragraphs[0].appliedParagraphStyle.name == 'PDF_name') {
pdf_name = frames[i].paragraphs[0].contents;
break;
}
}catch (e){
}
}
if (pdf_name == null) {
alert ('Error!\nThe Paragraph Style "PDF_name" is not applied to one or more of the pages or is Labeled Incorrectly.');
exit();
}
if(!pdf_name) continue;
}
var myPageNames = myGetPageNames();
var selectPgs = new Window ("dialog", "Select Page#'s To Export");
var pgGroup = selectPgs.add("group");
pgGroup.orientation = "column";
pgGroup.alignChildren = "center";
pgGroup.add("statictext", undefined, "Export Single Page pdf's");
var pgsList = pgGroup.add("listbox", [-25, 0, 170, 246], myPageNames, {multiselect: true});
var selectedpgsList = [];
for (var i = 0; i < pgsList.items.length; i++){selectedpgsList[-1] = pgsList.items[i];}
pgsList.selection = selectedpgsList;
var btn = selectPgs.add("group");
btn.orientation = "row";
btn.alignment = ["right", "top"];
var okBtn = btn.add ("button", undefined, "OK", {name:"OK"});
var cancelBtn = btn.add ("button", undefined, "Cancel", {name:"Cancel"});
var result = selectPgs.show();
if(result == 1){
}
else if (result== 2){
exit(0);
}
var pagelist = [];
if(pgsList.selection == null){
alert ("Error!\nNo page#'s were selected.")
exit();
}else{
for (var i=0; i<pgsList.selection.length; i++) {
pagelist.push(pgsList.selection[i].text);
}
}
for (i = 0; i < doc.pages.length; i++) {
if (String(pagelist).match("\\b"+doc.pages[i].name+"\\b")){
var pdf_name = getTextFromPageUsingParagraphStyle(doc.pages[i]);
app.pdfExportPreferences.pageRange = doc.pages[i].name;
var export_preset = app.pdfExportPresets.item("[High Quality Print]");
doc.exportFile(ExportFormat.PDF_TYPE, File(mySelectedFolder +"/"+ pdf_name + ".pdf"), false, export_preset);
}
}
alert("Done Exporting Pdf's!");
}
Regards,
Mike
Copy link to clipboard
Copied
Wonderful!!
Copy link to clipboard
Copied
Hi Mike,
How do you save the script to the Indesign Scripts folder? Thanks!
Copy link to clipboard
Copied
Hello @susaneala,
Saving the script file: Copy the code above into the text edit app setting the format to "Make Plain Text" then save the file with the .jsx extension.
Installing the Script file: in InDesign go to (Window > Utilities > Scripts), then right-click on the User folder, inside the folder that opens there is a folder called Scripts Panel. Put the script inside that folder.
Regards,
Mike
Copy link to clipboard
Copied
Thanks!
Copy link to clipboard
Copied
I signed up just so I could thank you for this script!
Does exactly what I needed it to and your instructions made it easy to implement.
Thanks so much for taking the time to share with the community!\
{{insert jumping high five emoji here }}
-=b=-
Copy link to clipboard
Copied
Hi Mike,
This works perfectly, but one question, I have hyperlinks on the indesign document linked to websites for help sections. Is there any way to run this script but to make the hyperlinks clickable as well?
Copy link to clipboard
Copied
Hello @Taylor25383048qtfc,
you will need to create a Adobe PDF Preset that meets your export requirements and includes Hyperlinks, then change the line of code below to match the name of your preset.
//Change this line
var export_preset = app.pdfExportPresets.item("[High Quality Print]");
//Change to
var export_preset = app.pdfExportPresets.item("insert the name of your preset here");
Regards,
Mike
Copy link to clipboard
Copied
This worked perfectly, thank you so much!!
Copy link to clipboard
Copied
Hi @Taylor25383048qtfc ,
Glad to hear that. Feel free to reach out if you have any other questions or issues.
Regards
Rishabh
Copy link to clipboard
Copied
Hi Mike,
Your work is greatly appreciated! However, I encounter an JavaScript Error in line 152 when running the script
Source: doc.exportFile(ExportFormat.PDF_TYPE, File(mySelectedFolder +"/"+ pdf_name + ".pdf"), false, export_preset);
Can you please advise?
Copy link to clipboard
Copied
Hello @Grafiker S&M
Can you post a screen shot of the error message and the script you're using if you made any modifications.
Regards,
Mike
Copy link to clipboard
Copied
Hi @Mike Bro , I got the same error. I didn't make any modifications to the script.
I do have hyperlinks to images on my file.
Copy link to clipboard
Copied
@Daniella31935498e168
Purchase Collins plugin script, it works great and is very flexible. I manage 30 files x 56 sales names and contacts. I also use it for business card layouts. I update my spreadsheet, export from google sheets, change extension to txt, open indesign, update link and datamerge with Collin's tool. I can custom name, hyperlinks work fine... it is a lifesaver and wasn't expensive. Hope this helps.
Copy link to clipboard
Copied
Thank you!
I did get Colin's script, the free version, but it doesn't work for what I want.
I have a multiple to multiple relationship with my records (multiple fund numbers and multiple individual IDs) and I want to combine them by IDs, with the resulting product being all the funds assigned to a specific ID in a single pdf.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
There is various ways to do this... and various script also.
You can also use Acrobat to split them using top level bookmarks generated by InDesign.
Take a look at this article:
https://creativepro.com/data-merging-individual-records-separate-pdfs/
Copy link to clipboard
Copied
I wrote a script that does exactly this.
it's available for FREE at https://colecandoo.com/scripts/ OR via the adobe add-ons https://exchange.adobe.com/creativecloud.details.100404.data-merge-to-single-records.html
Copy link to clipboard
Copied
Thanks!
Copy link to clipboard
Copied
Copy link to clipboard
Copied
I've seen this once before. The script is designed to work with delimiters that are either tabs or commas, but it looks like your data is separated by semicolons - that's indicated by the line being
modelName;bodyName;price;
To be clear, while InDesign's data merge can handle semicolons, my script hasn't been updated to reflect this change.
Open your text in a texteditor such as BBedit, notepad or similar, and check the delimiters. If they are semicolons, that'll be the issue, whether your file is a csv or txt. If they are semicolons, change them to tabs using find/change; and make sure the file has a .txt extension on the end. In Data Merge, then go to Select Data Source and make sure the "show import options" dialog box is checked on and then select this data.
HTH
Copy link to clipboard
Copied
Thank You!!!
Is there a way to add a base to the filename without it being in the csv file. I would like to set the basename in the dialog box and ADD <<name>> to the end of the file name. Like CC-NationalServices-<<name>>.pdf
I have 10 files I need to export all with different file names w/ the sales persons name added to the end. I guess I could have 10 csv files or 1 with 10 extra columns for the various filenames.
Or I could just be happy with what you have done and use a file renamed to prepend the pdf files.
Let me know if you have a solution for this.