topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday October 31, 2024, 7:08 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

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

jity2

  • Charter Member
  • Joined in 2006
  • ***
  • default avatar
  • Posts: 126
    • View Profile
    • Donate to Member
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. ;(

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.donation...ex.php?topic=27311.0, PDFInfoGUI http://skwire.dcmemb.../fp/?page=pdfinfogui both from skwire and this command line script WINDOWS+R : https://www.donation....msg330784#msg330784

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
NOTE: Only written for the OP's original purpose - requires SumatraPDF and Bullzip.

In Powershell, run it using the shortcut:

Code: PowerShell [Select]
  1. <#
  2.   PrintPDFs.ps1
  3.  
  4.   Print PDFs to default Bullzip PDF printer using SumatraPDF
  5. #>
  6.  
  7. Function Get-Folder {
  8.   Add-Type -AssemblyName System.Windows.Forms
  9.   $FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
  10.   [void]$FolderBrowser.ShowDialog()
  11.   $temp = $FolderBrowser.SelectedPath
  12.   If($temp -eq '') {Exit}
  13.   If(-Not $temp.EndsWith('\')) {$temp = $temp + '\'}
  14.   Return $temp
  15. }  
  16.  
  17. If($PSVersionTable.PSVersion.Major -lt 3) {
  18.   Write-Host '** Script requires at least Powershell V3 **'
  19. } else {
  20.   Write-Host 'Choose folder with PDF files: ' -NoNewline -BackgroundColor DarkGreen -ForegroundColor White
  21.   $srcFolder = (Get-Folder)
  22.   Write-Host $srcFolder
  23.   Write-Host 'Choose output folder: ' -NoNewline -BackgroundColor DarkGreen -ForegroundColor White
  24.   Do {$dstFolder = (Get-Folder)} While($dstFolder -eq $srcFolder)
  25.   Write-Host $dstFolder
  26.  
  27.   $aFiles = (Get-ChildItem -Include *.pdf -Path ($srcFolder + "*") -Recurse)
  28.   for($i=0; $i -lt $aFiles.Count; $i++) {
  29.     $inFile = [string]$aFiles[$i]
  30.     Write-Host 'File:' $inFile -BackgroundColor DarkBlue -ForegroundColor Yellow
  31.     $outFile = '  output=' + $dstFolder + $inFile.Replace($srcFolder, "")
  32.     Write-Host '-->' $outFile -BackgroundColor DarkBlue -ForegroundColor White
  33.     (Get-Content "$Env:APPDATA\PDF Writer\Bullzip PDF Printer\settings.ini") `
  34.       -Replace "(^..output=.+\.pdf$)", $outFile | `
  35.       Out-File "$Env:LOCALAPPDATA\PDF Writer\Bullzip PDF Printer\runonce.ini"
  36.     Start-Sleep -Seconds 2
  37.     $args = '-print-to-default -silent "' + $inFile + '"'
  38.     Start-Process -FilePath "C:\Program Files (x86)\SumatraPDF\SumatraPDF.exe" -Wait -ArgumentList $args
  39.   }
  40. }
  41. Write-Host ''
  42. Write-Host 'Close window to exit ...'
  43. cmd /c pause | out-null

First turn off Print Spooling in the Printer Properties:

Bullzip PDF Printer Properties.pngDONE: batch print silently pdf-2-pdf using SumatraPdf and Bullzip

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

  • Run the script from the shortcut
  • Select the source folder (where the PDFs are)
  • 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.
« Last Edit: June 09, 2016, 03:31 AM by 4wd »

jity2

  • Charter Member
  • Joined in 2006
  • ***
  • default avatar
  • Posts: 126
    • View Profile
    • Donate to Member
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.techrepub...-to-specific-folder/) ?

Many thanks in advance ;)

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
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"
« Last Edit: June 07, 2016, 07:47 AM by 4wd »

jity2

  • Charter Member
  • Joined in 2006
  • ***
  • default avatar
  • Posts: 126
    • View Profile
    • Donate to Member
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

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
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.
« Last Edit: June 08, 2016, 02:47 AM by 4wd »

jity2

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

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,287
    • View Profile
    • Donate to Member
Well done, 4wd.  I'll mark this as done and move it.

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Thanks skwire  :)

Updated