11

Is there any tool which I can run on the PDFs archives (all directories) and finally it lists/identifies the corrupt/invalid PDFs?

I have hundreds of PDF files (related to documentation etc.) on my computer (Windows machine), and very often I receive/have to send dozens of PDFs via email. It is now a normal routine that the PDF I receive or send is corrupted. This creates serious trouble sometimes, when the source file (e.g. Word file or the Tex file is missing/not available instantly).

Checking these thousands of PDFs is not possible in finite time, so I searched for a tool which I can run once and it scans all PDFs (in directories and sub-directories), and finally I get a list of those files which I should re-create. Upto now, it seems there is no such tool.

6

It is quite easy to check if a PDF file is valid, by using PDFtk. A free GUI for PDFtk is available from PDF Labs. When you run this tool, you can load as many PDFs as you wish, from multiple directories (by using Add files button), and then it will start accessing the pages in these PDF files, very quickly.

If any file among the selected PDFs is not valid PDF, this utility will show a message about the error, and will remove it automatically from the selection window.

Hence you can save many hours using this procedure with PDFtk. Additionally, if you have multicore CPU, you can run multiple instances of this utility and throw in hundreds of PDFs in each instance.

I am using this software since last 1 year, and it is the most handy PDF tool I have ever used.

| improve this answer | |
  • 2
    Alternatively, using the tool (pdfinfo.exe) available from the link mentioned by marcwho, you can cd into FolderContainingPDFs and run following command in Windows shell, and it will mark the invalid PDF file in a log file: FORFILES /S /M *.pdf /C "cmd /c echo. & echo @path @fname & D:\XPDF_3.04\bin64\pdfinfo.exe @file" 1>text.txt 2>&1 – Mubeen Shahid Jun 26 '14 at 15:14
4

I've used "pdfinfo.exe" from xpdfbin-win package and cpdf.exe to check PDF files for corruption, but didn't want to involve a binary if it wasn't necessary.

I read that newer PDF formats have a readable xml data catalog at the end, so I opened the PDF with regular windows NOTEPAD.exe and scrolled down past the unreadable data to the end and saw several readable keys. I only needed one key, but chose to use both CreationDate and ModDate.

The following Powershell (PS) script will check ALL the PDF files in the current directory and output the status of each into a text file (!RESULTS.log). It took about 2 minutes to run this against 35,000 PDF files. I tried to add comments for those who are new to PS. Hope this saves someone some time. There's probably a better way to do this, but this works flawlessly for my purposes and handles errors silently. You might need to define the following at the beginning: $ErrorActionPreference = "SilentlyContinue" if you see errors on screen.

Copy the following into a text file and name it appropriately (ex: CheckPDF.ps1) or open PS and browse to the directory containing the PDF files to check and paste it in the console.

#
# PowerShell v4.0
#
# Get all PDF files in current directory
#
$items = Get-ChildItem | Where-Object {$_.Extension -eq ".pdf"}

$logFile = "!RESULTS.log"
$badCounter = 0
$goodCounter = 0
$msg = "`n`nProcessing " + $items.count + " files... "
Write-Host -nonewline -foregroundcolor Yellow $msg
foreach ($item in $items)
{
    #
    # Suppress error messages
    #
    trap { Write-Output "Error trapped"; continue; }

    #
    # Read raw PDF data
    #
    $pdfText = Get-Content $item -raw

    #
    # Find string (near end of PDF file), if BAD file, ptr will be undefined or 0
    #
    $ptr1 = $pdfText.IndexOf("CreationDate")
    $ptr2 = $pdfText.IndexOf("ModDate")

    #
    # Grab raw dates from file - will ERR if ptr is 0
    #
    try { $cDate = $pdfText.SubString($ptr1, 37); $mDate = $pdfText.SubString($ptr2, 31); }

    #
    # Append filename and bad status to logfile and increment a counter
    # catch block is also where you would rename, move, or delete bad files.
    #
    catch { "*** $item is Broken ***" >> $logFile; $badCounter += 1; continue; }

    #
    # Append filename and good status to logfile
    #
    Write-Output "$item - OK" -EA "Stop" >> $logFile

    #
    # Increment a counter
    #
    $goodCounter += 1
}
#
# Calculate total
#
$totalCounter = $badCounter + $goodCounter

#
# Append 3 blank lines to end of logfile
#
1..3 | %{ Write-Output "" >> $logFile }

#
# Append statistics to end of logfile
#
Write-Output "Total: $totalCounter / BAD: $badCounter / GOOD: $goodCounter" >> $logFile
Write-Output "DONE!`n`n"
| improve this answer | |
3

Following the footsteps of @n0nuf, I wrote a batch script to check all PDFs in a specific folder with pdfinfo and push it through cpdf if broken as an attempt to fix them:

@ECHO OFF
FOR %%f in (*.PDF) DO (
    echo %%f
    pdfinfo "%%f" 2>&1 | findstr /I "error"  >nul 2>&1
    if not errorlevel 1 (
        echo "bad -> try to fix"
        @cpdf -i %%f -o %%f_.pdf 2>NUL
        mv %%f .\\bak\\%%f
    ) else (
       REM echo good        
    )
)
@ECHO ON

Or the same as bash script:

for file in $(find . -iname "*.pdf")
do
    echo "$file"
    pdfinfo "$file" 2>&1 | grep -i 'error' &> /dev/null
    if [ $? == 0 ]; then
       echo "broken -> try to fix"
       cpdf -i "$file" -o "$file"_.pdf
    fi
done

Broken PDFs will be moved to a subfolder \bak and the recreated PDFs get the suffix _.pdf (which is not perfect, but good enough for me). NOTE: A recreated PDF contains lesser errors and should be viewable with a regular PDF viewer. But this does not mean you get all your content back. Unrecoverable content leads to empty pages.

I also tried the same with JHOVE (Open source file format identification, validation & characterization tool) as suggested by @kraftydevil here: Check if PDF files are corrupted using command line on Linux and can now confirm this is also a valid approach. (First I had lesser success. But then I noticed I had not handled the output of JHOVE correctly.)

To test both approaches I deleted and altered random parts from a PDF with a text editor (removed streams, so pages failed to render in my PDF viewer, altered PDF Tags, and shifted some bits). The result is: Both pdfinfo and JHOVE are able to spot damaged files correctly (JHOVE was even more sensitive in some cases).

And here is the equivalent script for JHOVE:

@ECHO OFF
FOR %%f in (*.PDF) DO (
    echo %%f
    "C:\Program Files (x86)\JHOVE\jhove.bat" -m pdf-hul %%f | findstr /C:"Well-Formed and valid" >nul 2>&1
    if not errorlevel 1 (
        echo good
    ) else (
        echo "bad -> try to fix"
        @cpdf -i %%f -o %%f_.pdf 2>NUL
        REM mv %%f .\\bak\\%%f
    )
)
@ECHO ON
| improve this answer | |
0

There's also the (relatively new) pdfcpu library/tool, which has validation functionality:

pdfcpu validate whatever.pdf

Note that at the time of writing (August 2020) pdfcpu is still in Alpha stage.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.