I am trying to build a multipage PDF using Ghostscript by combining a list of single-page PDFs from their UNC file-paths (working in Python 3.7).
Here is the function:
import subprocess
import os
def ghostscript_merge_pdfs(in_PDF_list, out_PDF):
"""some doc string"""
# pdfPathsAsStr = '"' + ' "'.join(f'{pdf}"' for pdf in in_PDF_list)
pdfPathsAsStr = ' '.join(pdf for pdf in in_PDF_list)
print("The 'pdfPathsAsStr' variable is:")
print(pdfPathsAsStr + "\n")
args = [r"\\someDir\subDir\T\Tools\Ghostscript_Tools\GS_Install\gs9.54.0\bin\gswin64c",
'-sDEVICE=pdfwrite',
'-dNOPAUSE',
"-sOUTPUTFILE=" + out_PDF,
pdfPathsAsStr
]
p = subprocess.Popen(args, stdout=subprocess.PIPE)
print("\nCompleted: \n" + str(p.communicate()))
pdf_dir = r"\\someDir\subDir\T\Tools\Ghostscript_Tools\GS_Testing\IndividualPages"
out_pdf_path = os.path.join(pdf_dir, "Combo_PDF.pdf")
pdfs_list = [os.path.join(pdf_dir, "PDF_1.pdf"), os.path.join(pdf_dir, "PDF_2.pdf")]
ghostscript_merge_pdfs(pdfs_list, out_pdf_path)
The script outputs the following (note that slashes in pdfPathsAsStr
are not quadruplicated):
The 'pdfPathsAsStr' variable is:
\\someDir\subDir\T\Tools\Ghostscript_Tools\GS_Testing\IndividualPages\PDF_1.pdf \\someDir\subDir\T\Tools\Ghostscript_Tools\GS_Testing\IndividualPages\PDF_2.pdf
Completed:
(b'GPL Ghostscript 9.54.0 (2021-03-30)\nCopyright (C) 2021 Artifex Software, Inc. All rights reserved.\nThis software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:\nsee the file COPYING for details.\nError: /undefinedfilename in (\\\\\\\\someDir\\\\subDir\\\\T\\\\Tools\\\\Ghostscript_Tools\\\\GS_Testing\\\\IndividualPages\\\\PDF_1.pdf \\\\\\\\someDir\\\\subDir\\\\T\\\\Tools\\\\Ghostscript_Tools\\\\GS_Testing\\\\IndividualPages\\\\PDF_2.pdf)\nOperand stack:\n\nExecution stack:\n %interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push\nDictionary stack:\n --dict:732/1123(ro)(G)-- --dict:0/20(G)-- --dict:75/200(L)--\nCurrent allocation mode is local\nLast OS error: No such file or directory\n', None)
GPL Ghostscript 9.54.0: Unrecoverable error, exit code 1
I have looked a few places for help with UNC paths in Ghostscript, but can't find much help. I have tried a few variations of pdfPathsAsStr
inside the function, with no luck.
What am I doing wrong?
The following command outside Python runs for me no problem.
gswin64c -sDEVICE=pdfwrite -o"\\advent\share\Merged.pdf" "\\advent\share\cover.pdf" "\\advent\share\PDF files in a folder.pdf"
Showing that remote windows folders are not a problem for Ghostscript input or output.
Your problem is the way Python handles Windows pathing, and that can be minimised by reversing the folder names so that only the server name needs a \\prefix in Windows.
gswin64c -sDEVICE=pdfwrite -o"\\advent/share/Merged.pdf" "\\advent/share/cover.pdf" "\\advent/share/PDF files in a folder.pdf"
So in Python use \\\\
for server prefix when needed but use / in paths to make life easier (yes I know it's not best practice, but life is short and it's less RSI from the keyboard).
To test what cmd is getting from Python just run cmd /k echo "\\\\blah/blah"
as your executable command.