Jump to content

PDF combining


Recommended Posts

Because of coronavirus I have to make student packets every week.  It's roughly 10 pages front and back that each student gets a week. Those 10 pages are identical for every student.

However I need an individual cover sheet that has the student name and other data on it.  I use Mail Merge via Word for it, but I can export to a PDF.

I am paying for a copy place to staple them together for me, so what I need is 54 individual PDF files that have the 10 pages plus the unique cover page on top of it.   Any thoughts on how to accomplish this?

Link to comment
Share on other sites

1 hour ago, Danp2 said:

Why not give them two PDFs - one containing the 10 pages and the other containing the cover sheets? They could simply put a cover sheet with each packet and then staple them together.

They said they can't do it.  That it has to be a combined file.  They said they can't do cover sheets.

Link to comment
Share on other sites

So if I understand you correctly, you have a word document that contains the first 10 identical pages, plus a second word document that contains 54 different pages, one page per student.  You want to merge both documents and save 54 different PDF documents thru the print functionality of Word.  Is that it ?

Edited by Nine
Link to comment
Share on other sites

There are many command line tools available to combine PDFs.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

43 minutes ago, Nine said:

So if I understand you correctly, you have a word document that contains the first 10 identical pages, plus a second word document that contains 54 different pages, one page per student.  You want to merge both documents and save 54 different PDF documents thru the print functionality of Word.  Is that it ?

Not quite.

I have File A (cover page) that is 54 pages long, and each page is unique to student name.  One page needs to be distributed to each set of the File B for each student packet.
I have File B that is ten pages long (body of the packet).  Those need to be copied 54 times and have a cover page (File A) put on them.

Student #1 gets:
Cover Page #1 - File A (unique page)
Body of the Packet - File B (not unique)

Student #54 gets:
Cover Page #54 - File A (unique page)
Body of the Packet - File B (not unique)

 

 

 

Link to comment
Share on other sites

I was understanding it correctly. What I need to know is the format of File A and File B.  Cause if they are MS-Word documents, the script would be totally different from PDF documents.  

Link to comment
Share on other sites

22 minutes ago, Nine said:

I was understanding it correctly. What I need to know is the format of File A and File B.  Cause if they are MS-Word documents, the script would be totally different from PDF documents.  

Oh sorry, they can both be PDF if that is easier.  Flie A is a .docx that I convert to PDF for printing purposes, but whatever is easier.  File B is a PDF.  

Link to comment
Share on other sites

I use PDFSam too - great tool that does exactly what you need :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I use the command line version from the author of PDFSam:

https://sejda.org/

An example of addbackpages command:

"Takes one or more pages from a PDF document and adds them to one or more PDF documents after each 'n' pages."

#include <Constants.au3>

Global $iPID, $sOutput = ""

;$iPID = Run(@ComSpec & " /C " & '"' & @ScriptDir & '\sejda-console-2.10.4\bin\sejda-console.bat" merge -h', "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
;$iPID = Run(@ComSpec & " /C " & '"' & @ScriptDir & '\sejda-console-2.10.4\bin\sejda-console.bat" merge -l C:\files.csv -o C:\output.pdf --overwrite', "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

Global $sFolder = "C:\escaner\", $sFileName = '', $sText = ''

;Assign a Local variable the search handle of all files in the directory.
Local $hSearch = FileFindFirstFile($sFolder & "*.pdf")
;Check if the search was successful, if not display a message and exit
If $hSearch = -1 Then
  MsgBox($MB_SYSTEMMODAL, "", "Error: No files/directories matched the search pattern.")
  Exit
EndIf

While 1
   $sFileName = FileFindNextFile($hSearch)
   ; If there is no more file matching the search.
   If @error Then ExitLoop

   $sText &= " " & $sFolder & $sFileName
WEnd

;Close the search handle.
FileClose($hSearch)

$sText = StringMid($sText,2)

;$iPID = Run(@ComSpec & " /C """ & @ScriptDir & "\sejda-console-2.10.4\bin\sejda-console.bat"" merge -l C:\FILEST~1\files.csv -o C:\FILEST~1\output.pdf --overwrite", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
;$sFileShort= FileGetShortName($sFolder)
;$iPID = Run(@ComSpec & " /C """ & @ScriptDir & "\sejda-console-2.10.4\bin\sejda-console.bat"" merge -l " & $sFileShort & "files.csv -o " & $sFileShort & "output.pdf --overwrite", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
$iPID = Run(@ComSpec & " /C """ & @ScriptDir & "\sejda-console-2.10.4\bin\sejda-console.bat"" addbackpages -f " & $sText & " -b " & @ScriptDir & "\TEST.pdf -o " & $sFolder & " -j overwrite", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

ProcessWaitClose($iPID)
$sOutput = StdoutRead($iPID)
ConsoleWrite($sOutput)

;another option
;While 1
;  $sOutput &= StdoutRead($iPID, False, False)
;  If @error Then
;     ExitLoop
;  EndIf
;  Sleep(10)
;WEnd
;ConsoleWrite($sOutput)

;another option
;While 1
;    $sOutput = StdoutRead($iPID)
;    If @error Then ExitLoop;    ConsoleWrite($sOutput)
;WEnd

 

An example of simplesplit

#include <Constants.au3>

Global $iPID, $sOutput = ""

;$iPID = Run(@ComSpec & " /C " & '"' & @ScriptDir & '\sejda-console-2.10.4\bin\sejda-console.bat" merge -h', "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
;$iPID = Run(@ComSpec & " /C " & '"' & @ScriptDir & '\sejda-console-2.10.4\bin\sejda-console.bat" merge -l C:\files.csv -o C:\output.pdf --overwrite', "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

Global $sFolder = "C:\escaner\", $sFileName = '', $sText = ''

;Assign a Local variable the search handle of all files in the directory.
Local $hSearch = FileFindFirstFile($sFolder & "*.pdf")
;Check if the search was successful, if not display a message and exit
If $hSearch = -1 Then
  MsgBox($MB_SYSTEMMODAL, "", "Error: No files/directories matched the search pattern.")
  Exit
EndIf

While 1
   $sFileName = FileFindNextFile($hSearch)
   ; If there is no more file matching the search.
   If @error Then ExitLoop

   $sText &= " " & $sFolder & $sFileName
WEnd

;Close the search handle.
FileClose($hSearch)

$sText = StringMid($sText,2)

$iPID = Run(@ComSpec & " /C """ & @ScriptDir & "\sejda-console-2.10.4\bin\sejda-console.bat"" simplesplit -f " & $sText & " -o " & $sFolder & " -s all -j overwrite", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

ProcessWaitClose($iPID)
$sOutput = StdoutRead($iPID)
ConsoleWrite($sOutput)

 

Link to comment
Share on other sites

  • 2 weeks later...

Hi,

IMHO pdftk is really easy to use from AutoIT:

 

DESCRIPTION
       If PDF is electronic paper, then pdftk is an electronic staple-remover,
       hole-punch, binder, secret-decoder-ring, and X-Ray-glasses.  Pdftk is a
       simple tool for doing everyday things with PDF documents.  Use it to:

       * Merge PDF Documents or Collate PDF Page Scans
       * Split PDF Pages into a New Document
       * Rotate PDF Documents or Pages
       * Decrypt Input as Necessary (Password Required)
       * Encrypt Output as Desired
       * Fill PDF Forms with X/FDF Data and/or Flatten Forms
       * Generate FDF Data Stencils from PDF Forms
       * Apply a Background Watermark or a Foreground Stamp
       * Report PDF Metrics, Bookmarks and Metadata
       * Add/Update PDF Bookmarks or Metadata
       * Attach Files to PDF Pages or the PDF Document
       * Unpack PDF Attachments
       * Burst a PDF Document into Single Pages
       * Uncompress and Re-Compress Page Streams
       * Repair Corrupted PDF (Where Possible)

 

this is a small part of a script written to recursively split a lot of multipage PDF files to single page PDFs:

 

Func SplitPDF($_FullPathFileName)
    ; Syntax: pdftk <input.pdf> burst output <FileName_[format-spec].pdf>
    ; Example: pdftk c:\temp\PDF-Dok-A.pdf burst output c:\temp\PDF-Dok-A_%02d.pdf (_%02d will insert before the EXT: "_01", "_02", ...
    $pdftk = "pdftk.exe" ; installed using pdftk_server-2.02-win-setup.exe
    $pdftkDir = "C:\Program Files (x86)\PDFtk Server\bin"

    Local $RegExPathFnExt = "(?i)(^.*\\)(.*)(\.pdf$)" ; $1 = Path with trailing BACKSLASH, $2= FileName, $3 = ".PDF"
    Local $_Path = StringRegExpReplace($_FullPathFileName, $RegExPathFnExt, "$1")
    Local $_FN = StringRegExpReplace($_FullPathFileName, $RegExPathFnExt, "$2")
    Local $_Ext = StringRegExpReplace($_FullPathFileName, $RegExPathFnExt, "$3")
    Local $_pdftkParam = ' "' & $_FullPathFileName & '" burst output "' & $PDFOutDir & "\" & $_FN & "_%02d" & $_Ext & '"'
    Local $PID_TifSplit = Run(@ComSpec & " /c " & $pdftk & $_pdftkParam, $pdftkDir, @SW_SHOW, $STDERR_MERGED) ; $STDERR_MERGED --> chatch both, STDOUT and STDERR messages
    Local $pdftkTXT = ""
    While ProcessExists($PID_TifSplit)
        $pdftkTXT &= StdoutRead($PID_TifSplit)
    WEnd
    If StringStripWS($pdftkTXT, 8) = "" Then ; Keine Ausgaben außer WS --> kein Fehler
        ConsoleWrite("no errors recognized" & @CRLF)
    Else
        $ErrCount+=1
        $ErrTXT &= $_FN & $_Ext & @CRLF & $pdftkTXT & @CRLF & "--------------------------" & @CRLF
    EndIf
EndFunc   ;==>SplitPDF

 

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

I am not good at coding but someone helped me on my code so I would like to put my two cents here.

I think you can do it in easier way if above suggestion doesnt work.

  1. You can still use your mail merge to them and print then as PDF. Then you will end up with 540 pages of pdf file. 
  2. You can then use other software like PDF-XCHANGE-Viewer to extract those file by every 10th pages, then you will end up with 54 individual files and then save it.
  3. Then you can use simple autoit script to click windows button to save those 54 individual files

its much more easier. 

All the best!

Link to comment
Share on other sites

3 minutes ago, First_Project said:

I am not good at coding but someone helped me on my code so I would like to put my two cents here.

I think you can do it in easier way if above suggestion doesnt work.

  1. You can still use your mail merge to them and print then as PDF. Then you will end up with 540 pages of pdf file. 
  2. You can then use other software like PDF-XCHANGE-Viewer to extract those file by every 10th pages, then you will end up with 54 individual files and then save it.
  3. Then you can use simple autoit script to click windows button to save those 54 individual files

its much more easier. 

All the best!

Or after mail merge, you can set simple autoit script to save as pdf every 10th page on mail merged word file.

Link to comment
Share on other sites

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...