A Sabrina, unica vera mia grande passione.
Tech misc‎ > ‎

PdfMerge

Script in VBscript per eseguire il merge di più pdf.
Tutti in file pdf nella cartella di input, vengono accorpati in uno o più pdf.
Il paramertro cfgMaxPdf controlla il numero di pdf che devono essere contenuti in ogni pdf di output.
L'accorpamento è fatto usando uno "script" di comando in postscript.

Passi:
  • conversione dei file pdf in postscript (usando Xpdf)
  • costruzione dello "script" PS di comando
  • conversione postscript in pdf (usando GhostScript)

Esempio di "script" di comando PS:
/prun { /mysave save def
        dup = flush
        run
        clear cleardictstack
        mysave restore
} def
(D:/pdf_merge_xpdf_gs/TMP/file1.ps) prun
(D:/pdf_merge_xpdf_gs/TMP/file2.ps) prun
(D:/pdf_merge_xpdf_gs/TMP/file3.ps) prun

Prerequisiti:



Sorgente:


' --------------------------------------------------------
'
'   PDF Merge 0.1  (using xpdf.pdftops and ghostscript)
'
'   fhtino
'
' --------------------------------------------------------

cfgInputFolder  = "d:\Work_fabry\src\pdf_merge_xpdf_gs\IN\"
cfgOutputFolder = "d:\Work_fabry\src\pdf_merge_xpdf_gs\OUT\"
cfgTempFolder   = "d:\Work_fabry\src\pdf_merge_xpdf_gs\TMP\"
cfgPdfToPsExe   = "C:\Programmi\xpdf\xpdf-3.00-win32\pdftops.exe"
cfgGhostScrExe  = "C:\Programmi\gs\gs8.14\bin\gswin32c.exe"
cfgMaxPdf       = 10


Set objFSO=CreateObject("Scripting.FileSystemObject")

wscript.echo vbCrLf & "*** START ***" & vbCrLf


' --- delete temp and output files ---
objFSO.DeleteFile cfgOutputFolder & "*.*",true
objFSO.DeleteFile cfgTempFolder   & "*.*",true


' --- Conversion: pdf --> ps ---
wscript.echo "*** Conversion: PDF --> PS ***"
for each objFile in objFSO.GetFolder(cfgInputFolder).files
	if Ucase(Right(objFile.name,3))="PDF" then
		wscript.echo objFile.name
		if pdf2ps(cfgInputFolder & objFile.name, cfgTempFolder & objFile.name & ".ps")=false then
			wscript.quit(1)
		end if
	end if
next


' --- make PS command files ---
lotto=1
cnt=0
fileTxt=""
wscript.echo
wscript.echo "*** make PS command files ***"
for each objFile in objFSO.GetFolder(cfgTempFolder).files
	if Ucase(Right(objFile.name,2))="PS" then
		wscript.echo objFile.name		
		fileTxt = fileTxt & "(" & Replace(objFile.path,"\","/") & ") prun" & vbCrLf
		cnt=cnt+1
	end if
	if cnt=cfgMaxPdf then
		writePsRunFile fileTxt, cfgOutputFolder & "psrun_" & lotto & ".ps"
		fileTxt=""
		cnt=0
		lotto=lotto+1
	end if
next
if cnt<>0 then
	writePsRunFile fileTxt, cfgOutputFolder & "psrun_" & lotto & ".ps"
	fileTxt=""
	cnt=0
	lotto=lotto+1
end if


' --- conversion: PS --> PDF ---
i=0
wscript.echo
wscript.echo "*** conversion: PS --> PDF ***"
for i=1 to (lotto-1)
	outPdfFile="out_" & zeroFill(i,4) & ".pdf"
	wscript.echo outPdfFile
	if convertPS2PDFGhostScript(cfgOutputFolder & "psrun_" & i & ".ps", cfgOutputFolder & outPdfFile)<>0 then
		wscript.quit(1)
	end if
next


wscript.echo vbCrLf & "*** END ***" & vbCrLf

' ------------------------------------------------------------------------------

function zeroFill(x,n)
	dim i,s
	for i=(len(cstr(x))+1) to n
		s= s & "0"
	next
	zeroFill=s & cstr(x)
end function

' ------------------------------------------------------------------------------

function writePsRunFile(fileTxt,filePS)
	Dim objPSFile	
	Set objPSFile=objFSO.OpenTextFile(filePS,2,true)
	objPSFile.write "/prun { /mysave save def" & vbCrLf
	objPSFile.write "        dup = flush" & vbCrLf
	objPSFile.write "        run" & vbCrLf
	objPSFile.write "        clear cleardictstack" & vbCrLf
	objPSFile.write "        mysave restore" & vbCrLf
	objPSFile.write "} def" & vbCrLf
	objPSFile.write fileTxt	
	objPSFile.close	
	writePsRunFile=true
end function

' ------------------------------------------------------------------------------

function pdf2ps(inPdfFile,outPsFile)
	Dim txtExecCommand
	Dim objWshShell
	Dim intRC
	txtExecCommand = ""
	txtExecCommand = txtExecCommand & """" & cfgPdfToPsExe & """" & " "
	txtExecCommand = txtExecCommand & " -level2 -paper A4 "
	txtExecCommand = txtExecCommand & """" & inPdfFile & """" & " "
	txtExecCommand = txtExecCommand & """" & outPsFile & """" & " "
	set objWshShell = CreateObject("WScript.Shell")
	intRC=objWshShell.Run(txtExecCommand,7,true)
	set objWshShell = Nothing
	if intRC<>0 then
		wscript.echo "error: " & intRC
		pdf2ps=false
		exit function
	end if
	pdf2ps=true
end function

' ------------------------------------------------------------------------------

function convertPS2PDFGhostScript(byval psFile, byval pdfFile)
	Dim execString
	Dim objWshShell
	Dim intRC
	execString = cfgGhostScrExe
	execString = execString & " -sFONTPATH=c:\winnt\fonts\ "
	execString = execString & " -dNOPAUSE "
	execString = execString & " -q "
	execString = execString & " -dBATCH "
	execString = execString & " -sDEVICE=pdfwrite "
	execString = execString & " -dCompatibilityLevel=1.3 "
	execString = execString & " -dProcessColorModel=/DeviceCMYK "
	execString = execString & " -dEmbedAllFonts=true "
	execString = execString & " -dSubsetFonts=true "
	execString = execString & " -dAutoRotatePages=/None "
	execString = execString & " -dAutoFilterColorImages=false "
	execString = execString & " -dColorImageFilter=/FlateEncode "
	execString = execString & " -dEncodeColorImages=true "
	execString = execString & " -dColorConversionStrategy=/LeaveColorUnchanged "
	execString = execString & " -dDownsampleColorImages=false "
	execString = execString & " -dDownsampleGrayImages=false "
	execString = execString & " -dDownsampleMonoImages=false "
	execString = execString & " -r1200 "
	execString = execString & " -sOutputFile=" & """" & pdfFile & """"
	execString = execString & " " & """" & psFile & """"
	set objWshShell = CreateObject("WScript.Shell")
	intRC=objWshShell.Run(execString,7,true)
	set objWshShell = Nothing
	if intRC<>0 then
		wscript.echo "error: " & intRC
		convertPS2PDFGhostScript=1
		exit function
	end if
	convertPS2PDFGhostScript=0
end function

' ------------------------------------------------------------------------------