Concatenate two files to create new combined file

Post Reply
mclarke
Member
Posts: 54
Joined: Thu Feb 28, 2013 5:29 pm
Location: Syracuse, NY

Concatenate two files to create new combined file

Post by mclarke »

So, I have a need to create a PDF that contains a JDF file. The new file is for printing on a JDF compatible production printer, not for viewing in any PDF viewer. Merging the two files together is easy using the Windows "copy" command, but since I need to do this within a flow, I was wondering if anyone had a script or script expression that could be used to accomplish the task.

The JDF would need to be first, then the PDF. The files would be named exactly the same, except for the extension and could be in a job folder. The resulting file would be a PDF that would then be moved along the output connection to eventually be placed in a shared hotfolder for the device.

In a command prompt (run within the folder holding both files), the command would be:

COPY /B "<file_name>.jdf" + "<file_name>.pdf" /B "<file_name>.pdf" /B

Any assistance would be most appreciated. I didn't post in the NODE.js forum because I have not had the time yet to train myself how to use the new Node scripting method.
freddyp
Advanced member
Posts: 1008
Joined: Thu Feb 09, 2012 3:53 pm

Re: Concatenate two files to create new combined file

Post by freddyp »

You can also create a batch file with the Windows command that works and use that in Execute command.

If you prefer to script it the principle is (not tested):

Code: Select all

var jdf = File.readByteArray(jdfFilePath);
var pdf = File.readByteArray(pdfFilePath);
var both = new ByteArray(jdf, pdf);
File.writeByteArray(tempFilePath, both);
The use of ByteArray ensures that the files are treated as a bunch of files without any encoding. The NodeJS equivalent is Buffer.
mclarke
Member
Posts: 54
Joined: Thu Feb 28, 2013 5:29 pm
Location: Syracuse, NY

Re: Concatenate two files to create new combined file

Post by mclarke »

Thanks for the scripting info. Looks like trying a script is moving past my basic Javascript knowledge level.

So, I tried to use a batch file (tested in a cmd window and it works), but once I try to have Switch run the batch, it tries to process each file separately. I have had problems figuring out Execute Command in the past, but have always managed to get it to work, usually through trial and error.

This time, I can't for the life of me figure out how to make Switch work on both files, not each file independently. Here is the batch file:

Code: Select all

@echo off

COPY /B "*.jdf" + "*.pdf"
I call the batch file from it's directory, but then I'm not sure how the arguments are going to work. "%1" is only for one file. I've re-read the help page on Execute Command so many times I have a headache. I've tried submitting the files in a folder and that doesn't work, either. <sigh>

The errors all show Switch trying to work on each file independent of the other. When I put them both in a folder, I get different errors.

Single file errors (in that order and a set for each file:

Cannot complete sendToData() for 'C:/Users/Administrator/AppData/Roaming/Enfocus/Switch Server/temp/73/ScriptElement/56/112/ExecuteComandResult/v_34263_42690_W&J College Main Business Card_QTY_100.pdf'

Cannot send job: path 'C:/Users/Administrator/AppData/Roaming/Enfocus/Switch Server/temp/73/ScriptElement/56/112/ExecuteComandResult/v_34263_42690_W&J College Main Business Card_QTY_100.pdf' does not exist


When I send a folder, I get one set like above, but references the folder, no individual files. I'm at a loss on how to get this process to work.

Thanks in advance for any help.
jan_suhr
Advanced member
Posts: 586
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Concatenate two files to create new combined file

Post by jan_suhr »

You can do this with pdftk (pdftoolkit) running from the Execute Command. You will need the App Execute Command Friend to construct the batch file for it to work with pdftk.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
freddyp
Advanced member
Posts: 1008
Joined: Thu Feb 09, 2012 3:53 pm

Re: Concatenate two files to create new combined file

Post by freddyp »

It will not work with pdftoolkit, Jan, because mclarke is not concatenating 2 pdf's but a jdf and a pdf.

%1 is not for a single file, %1 is for the input job and that could just as well be a folder. So, make sure you have the PDF and the JDF in one folder (probably using Assemble job). Pass the input job folder and the output file as a parameter to your batch file. The Arguments in Execute command will be: "%1" "%2" and the output will be defined as a "File at path" with extension PDF.

In the batch file you then do this:

Code: Select all

REM Change the current directory to the input job folder
cd %1
REM Run the copy command on all JDF and PDF files (there will only be 1 of each)
REM and place the result in the output file which is the second parameter
COPY /B *.jdf /B *.pdf %2
Disclaimer: not tested!

Note that %1 in the Arguments and %1 in the batch file have a different meaning. In the Arguments it means the input job, in the batch file it means the first parameter. If you wrote "%2" 42 "%1" in the Arguments, then %1 in the batch file would be the output file path, %2 42 and %3 the input job path.
mclarke
Member
Posts: 54
Joined: Thu Feb 28, 2013 5:29 pm
Location: Syracuse, NY

Re: Concatenate two files to create new combined file

Post by mclarke »

Freddie, you are the MAN! Thanks so much!

I figured it needed to see the folder, but it didn't even occur to me to put a change directory command in the batch file. That is very useful info and I will definitely remember it for the future. The flow is now working correctly and I will go to work to add the Assemble folder part as well as a way to create the JDF dynamically.

Now, if I could just get my company to pay for those programming classes I asked for.... ;-)

Have a great weekend and thanks again!

Mike
Arrinters
Newbie
Posts: 2
Joined: Fri Jun 02, 2023 8:00 pm

Re: Concatenate two files to create new combined file

Post by Arrinters »

mike34 wrote: Tue Jun 06, 2023 6:32 am A useful addition is that a ">" overwrites the target-file with the source-files, whereas a ">>" appends the source-files to the target-file.
I try this and it works.
Post Reply