1

I have a large PDF file containing employee information. One page per employee, 500+ pages. I need to create a single PDF file per employee by splitting out this large file into smaller ones. I researched similiar questions regarding that subject here and found a answer that may help but I have run into a glitch of sorts.

While the code executes without errors and says it is processing the files, even giving the file name, it outputs nothing at all. by output I mean no files are created

I am using PDFtk to accomplish this on Powershell. Hopefully someone can point out why I am not getting any output.

this is my code

$pdfPath = 'C:\pdffiles\'
$pdfFile = Join-Path $pdfPath "empdf.pdf"
$OutDir = "C:\TestOutput\"
$SetsOfPages = 2
$NumberOfPages = 546
for ($Page=1;$Page -le $NumberOfPages;$Page+=$SetsOfPages){
  $File = Get-Item $pdfFile
  $Range = "{0}-{1}" -f $page,[math]::min($Page+$SetsOfPages-1,$NumberOfPages)
  $OutFile = Join-Path $OutDir ($File.BaseName+"_$Range.pdf")
  "processing: {0}" -f $OutFile
  pdftk $pdfFile cat $Range Burst output $OutFile
}

I have ensured that the pdftk executable file is in the proper location. The code runs without any displayed errors.

RobinOlsen
  • 33
  • 7
  • Just to rule out the obvious: Are you looking in `C:\TestOutput` for the output files? What exit code - as reflected in the [automatic `$LASTEXITCODE` variable](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Automatic_Variables#lastexitcode) - do the `pdftk` calls report? – mklement0 May 01 '23 at 22:27
  • FYI, with [QPDF](https://qpdf.readthedocs.io/en/stable/index.html), you can solve your problem with a simple, single line: `qpdf --split-pages empdf.pdf emp%d.pdf`. – stackprotector May 02 '23 at 05:17
  • mklement0 - LastExitCode is -1073741515 and the output directory is C:\TestOutput\ – RobinOlsen May 02 '23 at 17:07
  • K J - the pdftk.exe file is located in the main powershell directory (C:\Windows\system32\Windowspowershell\v1.0\) and it does not error out on execution so I am fairly sure that powershell knows where PDFTK.EXE is located. – RobinOlsen May 02 '23 at 17:37
  • @RobinOlsen, the error message means "The specified module could not be found" - https://www.hresult.info/FACILITY_NT/0xC0000135 – mklement0 May 02 '23 at 17:43
  • mklement0 Frustrating, the file pdftk.exe is definitely in a directory that that is included in the return values of $env:PSModulePath so now i am really confused as to why I get 0 files outputed at all even though it does process them. Thank you for your help and comments – RobinOlsen May 03 '23 at 16:44
  • My pleasure, @RobinOlsen. It is actually `$env:PATH` that matters here, but, yes, _locating_ the executable isn't the problem here. Rather, it seem like its installation is incomplete or got corrupted ("module" in the context of the cited error does _not_ refer to PowerShell modules). I'm not familiar with `pdftk.exe`, but you can try reinstalling it. – mklement0 May 03 '23 at 16:46

1 Answers1

1

"My pleasure, @RobinOlsen. It is actually $env:PATH that matters here, but, yes, locating the executable isn't the problem here. Rather, it seem like pdftk.exe's installation is incomplete or got corrupted ("module" in the context of the cited error (-1073741515, "The specified module could not be found.") does not refer to PowerShell modules).
I'm not familiar with pdftk.exe, but you can try reinstalling it."

–mklement0

mklement0
  • 382,024
  • 64
  • 607
  • 775
RobinOlsen
  • 33
  • 7
  • 1
    Thanks for posting an answer, but did you actually solve the problem, e.g, by reinstalling? – mklement0 May 04 '23 at 21:21
  • 1
    yes, that worked for me, I did not even install properly to begin with, just moved an exe file into the powershell path directory... I was originally getting an error that said the module was not found displayed, when I moved the file over to the powershell directory the error went away but it still did not create the files. I had to install the module. I am learning basic Powershell fairly quickly but third party modules I will have to learn how to deal with. – RobinOlsen May 04 '23 at 21:54