Jump to content

How to resend corrupt PDF files?


Recommended Posts

Hi!

I'm searching for a way to auto resend corrupt PDF files. I have a script (thanks matthew) that send pdf files via email to an user automaticly. But sometimes the user get a corrupt file from the script (don't know why).

Is there a way to autocheck files when he get one if the file is corrupt and when it is I need to send an information to the sender so the script can send the file again? This is the script:

#cs
   *Script should be placed in C:\scanner
   *Change Blowfish Password
   *Bfish must be in SendMail main directory (C:\scanner\sendmail)
   *GetMail.exe must be in GetMail main directory (C:\scanner\getmail)
#ce
#include<file.au3>
$g_szVersion = 'PDF Mailer'
If WinExists($g_szVersion) Then Exit; It's already running
AutoItWinSetTitle($g_szVersion)
If @WDAY = '1' OR @WDAY = '7' Then Exit
Opt ("RunErrorsFatal", 0)
Dim $oMyError = ObjEvent("AutoIt.Error", "ErrorHandler")
If @Compiled Then Opt ("TrayAutoPause", 0)
Global $Sender, $Recipient, $MailUser, $MailPass, $pdffile
If $CmdLine[0] > 0 Then
   If StringInStr($CmdLine[1], "deutschland") And StringInStr($CmdLine[1], "polen") Then Exit
   If StringInStr($CmdLine[1], "deutschland") Then
      $Sender = 'Emailadresse des Absenders'
      $Recipient = 'Emailadresse des Empfängers'
      $MailUser = 'Benutzername'
      $MailPass = 'Passwort'
   ElseIf StringInStr($CmdLine[1], "polen") Then
      $Sender = 'Emailadresse des Absenders'
      $Recipient = 'Emailadresse des Empfängers'
      $MailUser = 'Benutzername'
      $MailPass = 'Passwort'
   Else
      Exit
   EndIf
Else
   Exit
EndIf
#region Configuration Settings
Global $Blowfish_Password = 'Passwort für Blowfishverschlüsselung';For Blowfish
Global $POP3Server = 'POP3-Server'
Global $SMTPServer = 'SMTP-Server'
;SendMail Configuration
Global $SendMail = 'C:\scanner\sendmail'
Global $PDF_Dir = 'C:\scanner\sendmail\emails'
;GetMail Configuration
Global $GetMail = 'C:\scanner\getmail'

Global $source = 'C:\scanner\getmail\Emails'; Location of encrypted PDF files
Global $dest = 'C:\scanner\getmail\Emails\archives'; Location of decrypted PDF files
Global $GSPrintDir = 'C:\scanner\print\Ghostgum\gsview';Path to PDF Printer Program
#endregion Configuration Settings
#region Do Not Edit!!!!!
#region GetMail Functions
Func GetMail()
   If Not FileExists($source) Then DirCreate($source)
   If Not FileExists($dest) Then DirCreate($dest)
   FileChangeDir($GetMail)
   RunWait('getmail.exe -u ' & $MailUser & ' -pw ' & $MailPass & ' -s ' & $POP3Server & ' -xtract -port 110 -delete', '', @SW_HIDE)
   FileDelete('*.txt')
   FileDelete('*.out')
   FileMove('*.pdf', 'Emails\*.pdf', 1)
   Sleep(1000)
   $file = FileFindFirstFile($source & '\*.pdf')
   If $file = -1 Then Return 0
   While 1
      $pdffile = FileFindNextFile($file)
      If @error Then ExitLoop
      RunWait('bfish.exe /I:"' & $source & '\' & $pdffile & '" /O:"' & $dest & '\' & $pdffile & '" /P:"' & $Blowfish_Password & '" /D /Q', "", @SW_HIDE)
      If FileExists($dest & '\' & $pdffile) Then
        ;File successfully decrypted, print it!
         Local $WorkingDir = @WorkingDir
         FileChangeDir($GSPrintDir)
         RunWait('gsprint.exe "' & $dest & '\' & $pdffile & '"', $GSPrintDir, @SW_HIDE)
         If @error Then ErrorHandler($dest & '\' & $pdffile)
         FileChangeDir($WorkingDir)
      EndIf
      While FileExists($source & '\' & $pdffile)
         FileDelete($source & '\' & $pdffile);delete the encrypted version
      WEnd
   WEnd
   FileClose($file)
   Return 1
EndFunc  ;==>GetMail
#endregion
#region SendMail Functions
Func SendMail()
   If Not FileExists($PDF_Dir) Then DirCreate($PDF_Dir)
   If Not FileExists($PDF_Dir & '\Encrypted\') Then DirCreate($PDF_Dir & '\Encrypted\')
   If Not FileExists($PDF_Dir & '\Archives\') Then DirCreate($PDF_Dir & '\Archives\')
   If Not FileExists($PDF_Dir & '\*.pdf') Then Return 0
   Dim $PDFfiles[1]
   FileChangeDir($SendMail)
   $PDFHandle = FileFindFirstFile($PDF_Dir & '\*.pdf')
   While 1
      $file = FileFindNextFile($PDFHandle)
      If $file <> '' Then
         ReDim $PDFfiles[ (UBound($PDFfiles) + 1) ]
         $PDFfiles[0] += 1
         $PDFfiles[ (UBound($PDFfiles) - 1) ] = $file
      Else
         ExitLoop
      EndIf
   WEnd
   FileClose($PDFHandle)
   For $x = 1 To $PDFfiles[0]
      RunWait('bfish.exe /I:"' & $PDF_Dir & '\' & $PDFfiles[$x] & '" /O:"' & $PDF_Dir & '\Encrypted\' & $PDFfiles[$x] & '" /P:"' & $Blowfish_Password & '" /E /Q', "", @SW_HIDE)
      FileSend($PDFfiles[$x], $PDF_Dir & '\Encrypted\' & $PDFfiles[$x])
      FileMove($PDF_Dir & '\' & $PDFfiles[$x], $PDF_Dir & '\Archives\' & $PDFfiles[$x])
      FileDelete($PDF_Dir & '\Encrypted\' & $PDFfiles[$x])
   Next
   Return 1
EndFunc  ;==>SendMail
Func FileSend($FileName, $Attachment)
   $objMessage = ObjCreate('CDO.Message')
   With $objMessage
      .Subject = 'Betreff ' & $FileName
      .Sender = $Sender
      .From = $Sender
      .To = $Recipient
      .TextBody = $FileName & ' is attached and encrypted.'
      .AddAttachment ($Attachment)
   EndWith
   With $objMessage.Configuration.Fields
      .Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
      .Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $SMTPServer
      .Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
      .Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = $MailUser
      .Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $MailPass
      .Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
      .Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
      .Update
   EndWith
   $objMessage.Send
   Return 1
EndFunc  ;==>FileSend
#endregion
Func ErrorHandler($advanced = 0)
   If Hex($oMyError.number, 8) = "80020009" Then
      _FileWriteLog("C:\scanner\errorlog.log", $pdffile & " could not be sent to " & $Recipient)
   EndIf
   If $advanced <> 0 Then
      _FileWriteLog("C:\scanner\errorlog.log", $advanced & " could not be printed")
   EndIf
EndFunc  ;==>ErrorHandler
Func _ReduceMemoryUsage()
   Local $ai_GetCurrentProcessId = DllCall('kernel32.dll', 'int', 'GetCurrentProcessId')
   Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $ai_GetCurrentProcessId[0])
   Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
   DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
   Return $ai_Return[0]
EndFunc  ;==>_ReduceMemoryUsage
#endregion Do Not Edit
While 1
   If @Hour >= '07' AND @HOUR <= '16' Then
      GetMail()
      _ReduceMemoryUsage()
      Sleep(1000 * 5)
   Else
      Sleep(1000 * 5);pause anyways so it's not overloading the smtp server with messages, or taking up cpu resources
   EndIf
   SendMail()
   _ReduceMemoryUsage()
WEnd
Link to comment
Share on other sites

Hi!

I'm searching for a way to auto resend corrupt PDF files. I have a script (thanks matthew) that send pdf files via email to an user automaticly. But sometimes the user get a corrupt file from the script (don't know why).

Is there a way to autocheck files when he get one if the file is corrupt and when it is I need to send an information to the sender so the script can send the file again? This is the script:

#cs
   *Script should be placed in C:\scanner
   *Change Blowfish Password
   *Bfish must be in SendMail main directory (C:\scanner\sendmail)
   *GetMail.exe must be in GetMail main directory (C:\scanner\getmail)
#ce
#include<file.au3>
$g_szVersion = 'PDF Mailer'
If WinExists($g_szVersion) Then Exit; It's already running
AutoItWinSetTitle($g_szVersion)
If @WDAY = '1' OR @WDAY = '7' Then Exit
Opt ("RunErrorsFatal", 0)
Dim $oMyError = ObjEvent("AutoIt.Error", "ErrorHandler")
If @Compiled Then Opt ("TrayAutoPause", 0)
Global $Sender, $Recipient, $MailUser, $MailPass, $pdffile
If $CmdLine[0] > 0 Then
   If StringInStr($CmdLine[1], "deutschland") And StringInStr($CmdLine[1], "polen") Then Exit
   If StringInStr($CmdLine[1], "deutschland") Then
      $Sender = 'Emailadresse des Absenders'
      $Recipient = 'Emailadresse des Empfängers'
      $MailUser = 'Benutzername'
      $MailPass = 'Passwort'
   ElseIf StringInStr($CmdLine[1], "polen") Then
      $Sender = 'Emailadresse des Absenders'
      $Recipient = 'Emailadresse des Empfängers'
      $MailUser = 'Benutzername'
      $MailPass = 'Passwort'
   Else
      Exit
   EndIf
Else
   Exit
EndIf
#region Configuration Settings
Global $Blowfish_Password = 'Passwort für Blowfishverschlüsselung';For Blowfish
Global $POP3Server = 'POP3-Server'
Global $SMTPServer = 'SMTP-Server'
;SendMail Configuration
Global $SendMail = 'C:\scanner\sendmail'
Global $PDF_Dir = 'C:\scanner\sendmail\emails'
;GetMail Configuration
Global $GetMail = 'C:\scanner\getmail'

Global $source = 'C:\scanner\getmail\Emails'; Location of encrypted PDF files
Global $dest = 'C:\scanner\getmail\Emails\archives'; Location of decrypted PDF files
Global $GSPrintDir = 'C:\scanner\print\Ghostgum\gsview';Path to PDF Printer Program
#endregion Configuration Settings
#region Do Not Edit!!!!!
#region GetMail Functions
Func GetMail()
   If Not FileExists($source) Then DirCreate($source)
   If Not FileExists($dest) Then DirCreate($dest)
   FileChangeDir($GetMail)
   RunWait('getmail.exe -u ' & $MailUser & ' -pw ' & $MailPass & ' -s ' & $POP3Server & ' -xtract -port 110 -delete', '', @SW_HIDE)
   FileDelete('*.txt')
   FileDelete('*.out')
   FileMove('*.pdf', 'Emails\*.pdf', 1)
   Sleep(1000)
   $file = FileFindFirstFile($source & '\*.pdf')
   If $file = -1 Then Return 0
   While 1
      $pdffile = FileFindNextFile($file)
      If @error Then ExitLoop
      RunWait('bfish.exe /I:"' & $source & '\' & $pdffile & '" /O:"' & $dest & '\' & $pdffile & '" /P:"' & $Blowfish_Password & '" /D /Q', "", @SW_HIDE)
      If FileExists($dest & '\' & $pdffile) Then
        ;File successfully decrypted, print it!
         Local $WorkingDir = @WorkingDir
         FileChangeDir($GSPrintDir)
         RunWait('gsprint.exe "' & $dest & '\' & $pdffile & '"', $GSPrintDir, @SW_HIDE)
         If @error Then ErrorHandler($dest & '\' & $pdffile)
         FileChangeDir($WorkingDir)
      EndIf
      While FileExists($source & '\' & $pdffile)
         FileDelete($source & '\' & $pdffile);delete the encrypted version
      WEnd
   WEnd
   FileClose($file)
   Return 1
EndFunc  ;==>GetMail
#endregion
#region SendMail Functions
Func SendMail()
   If Not FileExists($PDF_Dir) Then DirCreate($PDF_Dir)
   If Not FileExists($PDF_Dir & '\Encrypted\') Then DirCreate($PDF_Dir & '\Encrypted\')
   If Not FileExists($PDF_Dir & '\Archives\') Then DirCreate($PDF_Dir & '\Archives\')
   If Not FileExists($PDF_Dir & '\*.pdf') Then Return 0
   Dim $PDFfiles[1]
   FileChangeDir($SendMail)
   $PDFHandle = FileFindFirstFile($PDF_Dir & '\*.pdf')
   While 1
      $file = FileFindNextFile($PDFHandle)
      If $file <> '' Then
         ReDim $PDFfiles[ (UBound($PDFfiles) + 1) ]
         $PDFfiles[0] += 1
         $PDFfiles[ (UBound($PDFfiles) - 1) ] = $file
      Else
         ExitLoop
      EndIf
   WEnd
   FileClose($PDFHandle)
   For $x = 1 To $PDFfiles[0]
      RunWait('bfish.exe /I:"' & $PDF_Dir & '\' & $PDFfiles[$x] & '" /O:"' & $PDF_Dir & '\Encrypted\' & $PDFfiles[$x] & '" /P:"' & $Blowfish_Password & '" /E /Q', "", @SW_HIDE)
      FileSend($PDFfiles[$x], $PDF_Dir & '\Encrypted\' & $PDFfiles[$x])
      FileMove($PDF_Dir & '\' & $PDFfiles[$x], $PDF_Dir & '\Archives\' & $PDFfiles[$x])
      FileDelete($PDF_Dir & '\Encrypted\' & $PDFfiles[$x])
   Next
   Return 1
EndFunc  ;==>SendMail
Func FileSend($FileName, $Attachment)
   $objMessage = ObjCreate('CDO.Message')
   With $objMessage
      .Subject = 'Betreff ' & $FileName
      .Sender = $Sender
      .From = $Sender
      .To = $Recipient
      .TextBody = $FileName & ' is attached and encrypted.'
      .AddAttachment ($Attachment)
   EndWith
   With $objMessage.Configuration.Fields
      .Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
      .Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $SMTPServer
      .Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
      .Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = $MailUser
      .Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $MailPass
      .Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
      .Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
      .Update
   EndWith
   $objMessage.Send
   Return 1
EndFunc  ;==>FileSend
#endregion
Func ErrorHandler($advanced = 0)
   If Hex($oMyError.number, 8) = "80020009" Then
      _FileWriteLog("C:\scanner\errorlog.log", $pdffile & " could not be sent to " & $Recipient)
   EndIf
   If $advanced <> 0 Then
      _FileWriteLog("C:\scanner\errorlog.log", $advanced & " could not be printed")
   EndIf
EndFunc  ;==>ErrorHandler
Func _ReduceMemoryUsage()
   Local $ai_GetCurrentProcessId = DllCall('kernel32.dll', 'int', 'GetCurrentProcessId')
   Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $ai_GetCurrentProcessId[0])
   Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
   DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
   Return $ai_Return[0]
EndFunc  ;==>_ReduceMemoryUsage
#endregion Do Not Edit
While 1
   If @Hour >= '07' AND @HOUR <= '16' Then
      GetMail()
      _ReduceMemoryUsage()
      Sleep(1000 * 5)
   Else
      Sleep(1000 * 5);pause anyways so it's not overloading the smtp server with messages, or taking up cpu resources
   EndIf
   SendMail()
   _ReduceMemoryUsage()
WEnd

<{POST_SNAPBACK}>

just glancing over the code, it looks like this script just creates a new mail and attaches a file to it then sends. If the file is being corrupted in transit, that would be indicative of either an issue with your SMPTP service (part of TCP/IP protocol) or an issue on his side. as far as checking to see if the file is corrupted, the pdf file has it's own checks in place, that's actually what generates the error that the file is corrupt or invalid. One thing you could do is have a client side script on his side that opens the file after it's received, and then creates a mail back to the sender if the file doesn't open correctly... One thing i would suggest, especially if this is just an intermittent issue, is have your friend update their pdf reader to make sure it's at newest version, alot of times if there are features in the pdf that are not supported by the reader, it appears to be corrupted. Or if you really want to learn stuff just to learn stuff, you could research the DLL's for his reader, and see if you can't find the function that checks the file integrity, then implement that into a clientside script on his side...
Link to comment
Share on other sites

just glancing over the code, it looks like this script just creates a new mail and attaches a file to it then sends.    If the file is being corrupted in transit, that would be indicative of either an issue with your SMPTP service (part of TCP/IP protocol) or an issue on his side.  as far as checking to see if the file is corrupted, the pdf file has it's own checks in place, that's actually what generates the error that the file is corrupt or invalid.  One thing you could do is have a client side script on his side that opens the file after it's received, and then creates a mail back to the sender if the file doesn't open correctly...  One thing i would suggest, especially if this is just an intermittent issue, is have your friend update their pdf reader to make sure it's at newest version, alot of times if there are features in the pdf that are not supported by the reader, it appears to be corrupted.  Or if you really want to learn stuff just to learn stuff,  you could research the DLL's for his reader, and see if you can't find the function that checks the file integrity, then implement that into a clientside script on his side...

<{POST_SNAPBACK}>

Hi cameronsdad!

Both (the Sender and the Reciever) have the same Reader, its Ghostview. The PDF Files are still the same, just with another images in it (create by a documentscanner). Maybe 1 or 2 file of 10 are corrupt. Maybe its the SMTP Server, but the smtp server is on the same server like the pop3 server, so it will not send to another server.

You told the right, I searching for a methode to open the file in background and if it can't open it, just send a email to sender that the file is courrpt and should send again. Have you an idea how i can make it with Ghostview?

Link to comment
Share on other sites

Hi cameronsdad!

Both (the Sender and the Reciever) have the same Reader, its Ghostview. The PDF Files are still the same, just with another images in it (create by a documentscanner). Maybe 1 or 2 file of 10 are corrupt. Maybe its the SMTP Server, but the smtp server is on the same server like the pop3 server, so it will not send to another server.

You told the right, I searching for a methode to open the file in background and if it can't open it, just send a email to sender that the file is courrpt and should send again. Have you an idea how i can make it with Ghostview?

<{POST_SNAPBACK}>

sorry man, i don't know anything about ghostview at all, but if you have a script programatically altering the pdf file, then there is a possibility of the script being to blame for the corruption. are you able to see any likenesses between the failed files? file size, info added, time of day sent, etc? the reason i ask is that if you have a script adding data to the pdf, and only some of the files are failing, there may be a logic error somewhere in the script that is causing the corruption, and the easiest way to find the error is to figure out what could be causing a condition to evaluate incorrectly and corrupt the data...
Link to comment
Share on other sites

sorry man, i don't know anything about ghostview at all, but if you have a script programatically altering the pdf file, then there is a possibility of the script being to blame for the corruption.  are you able to see any likenesses between the failed files? file size, info added, time of day sent, etc? the reason i ask is that if you have a script adding data to the pdf, and only some of the files are failing, there may be a logic error somewhere in the script that is causing the corruption, and the easiest way to find the error is to figure out what could be causing a condition to evaluate incorrectly and corrupt the data...

<{POST_SNAPBACK}>

Sorry there is no logic error in the script that makes this corrupt pdf files :)
Link to comment
Share on other sites

  • 2 years later...

hello,

Does the emailing occur immediately after generation? Maybe the file

has not been fully written and closed before the email process grabs

it. If that's the issue, introducing a brief delay could solve the

problem. Or perhaps verify you can get a low-level lock on the file

before sending.

You can try a popular PDF file recovery tool called Advanced PDF Repair

to repair your PDF file. It is a powerful tool to repair corrupt or

damaged PDF files.

Detailed information about Advanced PDF Repair can be found at

http://www.datanumen.com/apdfr/

And you can also download a free demo version at

http://www.datanumen.com/apdfr/apdfr.exe

Alan

Link to comment
Share on other sites

Link to comment
Share on other sites

  • 4 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...