Great. I am glad you got it working.
Stefan
Good evening,
after tossing and turning, I got it working.
Apparently was the configuration of the BW conversion in the Manage Color component that wasn't the best.
I had noticed before that page 1 of my example file wasn't being converted from color to BW in the 'grey' document, but all the others were, so I didn't mind it much up until now, because I saw that in fact the newly inserted 'print' commands were logging the correct color/BW page mapping in stdout.
After correcting the Manage Color component and getting the 'grey' document fully converted to BW, the workflow started performing as expected.
Once again, thank you so much for your help and sorry for being such a python-rookie bugger. You rule!
Thanks
Best Regards
Nuno
Hi Nuno,
Could it be that the variable for the pages are not evaluated properly for some reason? Try adding print('color') or print('bw') inside the if/else statement like this:
for i in range(infile_color.getNumPages()):
if j in colorpages:
p = infile_color.getPage(i)
output.addPage(p)
j = j + 1
print('color')
else:
p = infile_gray.getPage(i)
output.addPage(p)
j = j + 1
print('bw')
Then check stdout for what has been written. If only 'color' has been printed then
colorpages = ast.literal_eval(sys.argv[4])
is not working for some reason.
Also comment out the lines
os.remove('C:/Out/' + sys.argv[3] + '.pdf')
os.remove('C:/Out/' + sys.argv[3] + '_gray.pdf')
to check what has been saved and that a conversion to grayscale actually has been done.
Stefan
Hello,
thanks for your input and for taking the time to look at this.
I tested the changed script and with the 'move' line I don't get any file moved to my 'out' folder, it's generated inside C:\Temp and then disappears.
If I comment (#) the 'move' line in the script the PDF stays inside C:\Temp, but it's the same as I had gotten before, it's an exact copy of the original file (no BW pages in the file).
Thanks
Best regards
Nuno
Try writing the pdf to some temp folder before sending it to $FFout$. Like this for instance to the folder C:/Temp/
import shutil
with open("C:/Temp/" + os.path.basename(sys.argv[1]), 'wb') as f:
output.write(f)
shutil.move("C:/Temp/" + os.path.basename(sys.argv[1]), sys.argv[2])
Please see modified script.
Stefan
Hello Stefan,
I'm testing and learning Python to apply with FFCore and came across your solution/script, I've installed the script and have tested it inumerous times but can't get the final file with the BW/Color pages combined.
The script runs without errors but I never get to see the final file.
I tried tweaking the output lines of the script to match my 'Out' folder but the final file ALWAYS ends up being a copy of the original 'color' file, never has any BW page.
I'm using a .csv with the files location and the pages to keep strings (e.g. [0, 2, 4]).
I see that a temporary folder is created in my 'Scripts' directory during execution but then it gets automatically deleted.
This is an example of the 'Out' log i get on success:
<stdout 09/03/2021 15:17:21>
C:\Xerox\FreeFlow\Core\00000000-0000-0000-0000-000000000000\Data\Scripts>"C:\Users\50544342\AppData\Local\Programs\Python\Python39\python.exe" "C:\Xerox\FreeFlow\Core\00000000-0000-0000-0000-000000000000\Data\Scripts\mergecolorandgray.py" "C:\Xerox\FreeFlow\Core\00000000-0000-0000-0000-000000000000\Platform\Spool\dce791a7-b2a8-4123-b5cd-8748cbe6aa57\Documento.pdf" "C:\Xerox\FreeFlow\Core\00000000-0000-0000-0000-000000000000\Data\Scripts\40cc19d2-fb4c-40c1-9172-74c70e2d4ee6" "Documento" "[1, 2, 3, 8]"
</stdout>
The temporary folder I mention above is the "$FFout$" parameter that translates in this case to the folder '40cc19d2-fb4c-40c1-9172-74c70e2d4ee6' inside the scripts directory.
Can you give me a clue, please?
Thanks in advance
Best regards
-N
You need to make sure the contents of the scripts I sent points to the correct locations. Find the latest 64-bit Windows installer for Python and when it is run select Customize installation. Then check the Add Python 3.x to PATH setting. In the Customize Installation location type C:\Program Files\Python38. This will install Python where the batch file points to python38.exe. Then from a command prompt as admin run:
pip install PyPDF2
Batch file mergecolorandgray.py.bat contains this:
"C:\Program Files\Python38\python.exe" "C:\Xerox\FreeFlow\Core\00000000-0000-0000-0000-000000000000\Data\Scripts\mergecolorandgray.py" %1 %2 %3 %4
and this is the script that runs in the External Process.
The batch file then calls the python script mergecolorandgray.py which assumes there is a folder C:\Out for temporary files. You could change it to C:\Temp if you like in the python script.
The manifest is a csv file that you need to create somehow. You submit the manifest to a hot folder but you need to do the MAX Setup on the hot folder to tell the system what each column in the manifest represents.
As an alternative to a manifest you could add a keyword to a pdf ([2,5,8] etc to tell the system what pages should not be grayscale) and then submit the pdf to a hotfolder or by any other supported method.
In the example I sent, the last stage is the External Process. After this stage you can add whatever component you need to get the file where you want - Print, Save etc.
I have attached a slightly modified script so that the pages can be entered in a way that makes sense (In the first one I sent page 1 is represented by 0, page 2 is represented by 1 and so on).
If you encounter any issues then please check the stderr.log and stdout.log files in the External Process Status folder in the FreeFlow Core Scripts folder.
Stefan
Hi Stefan,
I worked on your solution. I have installed python, PyPDF2 (hope I did it good). And nothing worked so I have some questions ( you must have to know that I'm really not a WF expert!!!):
1 - How can I know if python and PyPDF2 are working good ?
2 - Just to be sure : the "mergecolorandgray.py.bat" is the batch you called "the batch file looks like this" ?
3 - I have a misunderstanding with the manifest. What it a manifest : a txt file ? an excel file ? or what else ?
4 - How this manifest is created ? Do I have to create a special file ? If yes, where do I have to put it ?
After that, what is the procedure ? What file do we have to put in the flow ? And after proceeding, where can I find the final file ?
Thanks for your support.
Jerome
Hi Stefan,
many thanks for your answer. I'll leave for few weeks next days, so I'll test your solution when I'll be back.
Thank you for your time.
I don't know what would be your preferable way of telling the system what pages should be color or grayscale, but here is an idea.
Assume you have a manifest like this:
file;colorpages
C:/files/doc1.pdf;[0,2,4]
C:/files/doc2.pdf;[1,4,5,7]
where the first column is the pdf to use and the second one what pages should be color.
When you map this on the hot folder for a workflow, in the MAX Setup use the pdf as column A and map column B as a custom field. Here I called it pagerange. This will give you the variable $FFpagerange$ that will be used later.
A workflow like this:
First Save component saves the job to C:\Out\ with the job name.
Then a Manage Color component that converts to grayscale. Could be done with a Preflight also.
Then a second Save component to C:\Out\ with the job name but with the addition _gray.
Now you have both the original pdf and the grayscale version of it in C:\Out\. Now a new pdf will be created that uses both those files and the variable $FFpagerange$ to determine if a color page or a grayscale page should be added to the new pdf. This will be taken care of by the last component which is an External Process that uses a batch file with the parameters:
"$FFin$" "$FFout$" "$FFwfJob.jobName$" "$FFpagerange$"
The batch file looks like this:
"C:\Program Files\Python38\python.exe" "C:\Xerox\FreeFlow\Core\00000000-0000-0000-0000-000000000000\Data\Scripts\mergecolorandgray.py" %1 %2 %3 %4
The batch file calls a Python script that looks like this:
import sys
import os
import ast
from PyPDF2 import PdfFileWriter, PdfFileReader
colorpages = ast.literal_eval(sys.argv[4])
infile_color = PdfFileReader('C:/Out/' + sys.argv[3] + '.pdf', 'rb')
infile_gray = PdfFileReader('C:/Out/' + sys.argv[3] + '_gray.pdf', 'rb')
output = PdfFileWriter()
for i in range(infile_color.getNumPages()):
if i in colorpages:
p = infile_color.getPage(i)
output.addPage(p)
else:
p = infile_gray.getPage(i)
output.addPage(p)
with open(sys.argv[2] + '\\' + os.path.basename(sys.argv[1]), 'wb') as f:
output.write(f)
os.remove('C:/Out/' + sys.argv[3] + '.pdf')
os.remove('C:/Out/' + sys.argv[3] + '_gray.pdf')
What happens is that the Python script reads both saved pdf files. It then checks if the page is in $FFpagerange$. If it is, then a page from the original pdf is added and if not then a grayscale page is added. (Page counting starts with 0. If you would like to input page numbers that you are used to then the script has to modified.)
Both scripts have to be placed in the FreeFlow Core scripts folder. I have my scripts folder on C:\. If you have your scripts folder somewhere else then you need to change the drive letter/path in the batch file.
Also, this requires Python https://www.python.org/downloads/ to be installed on your FreeFlow Core server. After Python is installed you need to install PyPDF2. From command line as admin run:
pip install PyPDF2
Depending on where Python is installed you might need to change the path to python.exe in the batch file.
All done.
I have also attached both scripts.
If you dont' want to use a manifest for this, then you could for instance use a pdf keyword (or some other variable that could be included in the pdf itself) with the information of what pages should be in color.
You then need to replace "$FFpagerange$" with the variable "$FFwfdoc.Keywords$" in the External Process component.
/Stefan