DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: DONE: batch print silently pdf-2-pdf using SumatraPdf and Bullzip  (Read 18,831 times)

jity2

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 126
Dear all,

I have noticed that trying to OCR some of my PDF files causes a crash. But if I open the file with (and only!) SumatraPDF I can print the pdf to a pdf file (with bullzip) and get a new pdf file that I can then OCR successfully. ;)
So I need to print pdf-to-pdf many pdf files gathered inside one big folders and its many sub-folders.

I thought that an .ahk script would help me : I choose the folder where I have the original pdf files. It opens SumatraPDF silently, it logs the possible files in error then continue, and print to the default free printer Bullzip silently.
I have already chosen the default output folder and the silent part in the settings of Bullzip.

I have tried to adapt an old ahk file to my purpose, but it doesn't work. ;(

Code
FileSelectFolder,SourcePath,,0,Select Source Folder
	If SourcePath =
		ExitApp

Loop,%SourcePath%\*.*,2,0
{
	FolderSize = 0
	Loop, %A_LoopFileFullPath%\*.*, , 1
		FolderSize += %A_LoopFileSize%
	If FolderSize >0
		RunWait,C:\Program Files (x86)\SumatraPDF\SumatraPDF.exe  -print-to-default -silent "%A_LoopFileFullPath%\*.*"
}

Many thanks in advance ;)

ps: I also use these freeware a lot which helps reduce the list of pdf files which have problems with OCR : PDF Text Checker https://www.donationcoder.com/forum/index.php?topic=27311.0, PDFInfoGUI http://skwire.dcmembers.com/fp/?page=pdfinfogui both from skwire and this command line script WINDOWS+R : https://www.donationcoder.com/forum/index.php?topic=35339.msg330784#msg330784

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
NOTE: Only written for the OP's original purpose - requires SumatraPDF and Bullzip.

In Powershell, run it using the shortcut:

Code: ( powershell)
<#
  PrintPDFs.ps1
  
  Print PDFs to default Bullzip PDF printer using SumatraPDF
#>

Function Get-Folder {
  Add-Type -AssemblyName System.Windows.Forms
  $FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
  [void]$FolderBrowser.ShowDialog()
  $temp = $FolderBrowser.SelectedPath
  If($temp -eq '') {Exit}
  If(-Not $temp.EndsWith('\')) {$temp = $temp + '\'}
  Return $temp
}  

If($PSVersionTable.PSVersion.Major -lt 3) {
  Write-Host '** Script requires at least Powershell V3 **'
} else {
  Write-Host 'Choose folder with PDF files: ' -NoNewline -BackgroundColor DarkGreen -ForegroundColor White
  $srcFolder = (Get-Folder)
  Write-Host $srcFolder
  Write-Host 'Choose output folder: ' -NoNewline -BackgroundColor DarkGreen -ForegroundColor White
  Do {$dstFolder = (Get-Folder)} While($dstFolder -eq $srcFolder)
  Write-Host $dstFolder

  $aFiles = (Get-ChildItem -Include *.pdf -Path ($srcFolder + "*") -Recurse)
  for($i=0; $i -lt $aFiles.Count; $i++) {
    $inFile = [string]$aFiles[$i]
    Write-Host 'File:' $inFile -BackgroundColor DarkBlue -ForegroundColor Yellow
    $outFile = '  output=' + $dstFolder + $inFile.Replace($srcFolder, "")
    Write-Host '-->' $outFile -BackgroundColor DarkBlue -ForegroundColor White
    (Get-Content "$Env:APPDATA\PDF Writer\Bullzip PDF Printer\settings.ini") `
      -Replace "(^..output=.+\.pdf$)", $outFile | `
      Out-File "$Env:LOCALAPPDATA\PDF Writer\Bullzip PDF Printer\runonce.ini"
    Start-Sleep -Seconds 2
    $args = '-print-to-default -silent "' + $inFile + '"'
    Start-Process -FilePath "C:\Program Files (x86)\SumatraPDF\SumatraPDF.exe" -Wait -ArgumentList $args
  }
}
Write-Host ''
Write-Host 'Close window to exit ...'
cmd /c pause | out-null

First turn off Print Spooling in the Printer Properties:

Bullzip PDF Printer Properties.png

This is so a job finishes before the next is sent (was the easiest way to do it)

  1. Run the script from the shortcut
  2. Select the source folder (where the PDFs are)
  3. Select the destination folder

For each file it will rewrite the "output= ..." line in settings.ini (%APPDATA%\PDF Writer\Bullzip PDF Printer\settings.ini) and save the new file as runonce.ini (%LOCALAPPDATA%\PDF Writer\Bullzip PDF Printer\runonce.ini).  Bullzip uses this file in preference to settings.ini if it exists and deletes it after a job is finished.

Once it's finished you can re-enable Print Spooling for Bullzip.

I've included the Bullzip settings.ini file I was testing it with for comparison.

UPDATED (20160609):
  • Won't let you choose destination folder same as source folder, just loops until they are different.
  • Can now choose drive root of source folder for destination, (barring condition where they are both the same) - strips the source path from the file path/name so the output folder structure is a bit more normal, ie. the extra folder levels aren't output.
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903
« Last Edit: June 09, 2016, 08:31 AM by 4wd »

jity2

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 126
Hi 4wd,
Many thanks for your help. ;)

I tested your code and it is working. ;) Alas I have made an error : I can select the output folder in Bullzip but it does not create the same subfolders of the source by itself. It put all processed pdf files into the same folder. ;(
This is very annoying for me as I need this.
Any idea ? Maybe using another pdf printer (apparently Bullzip runs in part ghostscript ; I also found this link but I don't understand everything! http://www.techrepublic.com/forums/discussions/printing-microsoft-access-report-as-pdf-to-specific-folder/) ?

Many thanks in advance ;)

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
If you drop the -Recurse parameter from the following line:

$aFiles = (Get-ChildItem ...

then it will only do the folder you choose, no sub-folders.  You can then do a folder at a time changing the output folder for Bullzip as you go - slow but an easy way to do it.

I'll download Bullzip/SumatraPDF and play.

EDIT: Looks like you can specify the full output path/file for Bullzip's next job by using the config command it comes with, eg: config.exe /S "Output" "X:\output\<basedocname>.pdf"
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903
« Last Edit: June 07, 2016, 12:47 PM by 4wd »

jity2

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 126
Hi "4wd",

I haven't seen you edit before. Sorry. Thanks for your answer. ;)

Great find. My config is located at : C:\Program Files\Bullzip\PDF Printer\API\EXE\config.exe

So I guess I should add somewhere in your code the line :
C:\Program Files\Bullzip\PDF Printer\API\EXE\config.exe /S "Output_folder_or_subfolder" "I:\output_folder_or_subfolder\<basedocname>.pdf"


Also, would it be possible that the code sends only one file to be printed ?  Then it is printed by Bullzip. Then once bullzip has finished, the code sends another file to be printed ? I ask this because I have noticed that if I add many pdf to be printed, my computer becomes not responsive. I have checked and a 10 MB pdf file become easily a 2 GB or more (no typo!) file to be printed !

Thanks in advance ;)


4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
I've actually got it worked out, I'll update my post in a little while  ;)

Updated

Also, would it be possible that the code sends only one file to be printed ?  Then it is printed by Bullzip. Then once bullzip has finished, the code sends another file to be printed ?

Disabling the Print Spooler will stop SumatraPDF from just queuing up the print jobs, it has to wait until each job is finished before it can exit and run again.
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903
« Last Edit: June 08, 2016, 07:47 AM by 4wd »

jity2

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 126
Hi "4wd",
Many thanks. I did a few tests and this seems to be working great. ;)
I much appreciated. ;)
Thank you again ;)

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Well done, 4wd.  I'll mark this as done and move it.

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Thanks skwire  :)

Updated
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

Original address: https://www.donationcoder.com/forum/index.php?topic=42713.0