Skip to content

Instantly share code, notes, and snippets.

@mkoertgen
Last active July 9, 2025 15:00
Show Gist options
  • Select an option

  • Save mkoertgen/2087bacda9cff0fa68aa to your computer and use it in GitHub Desktop.

Select an option

Save mkoertgen/2087bacda9cff0fa68aa to your computer and use it in GitHub Desktop.

Revisions

  1. mkoertgen revised this gist Nov 24, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Doc2Pdf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    A powershell automating Word to generate Pdf
  2. mkoertgen revised this gist Oct 1, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    *.docx
    *.docx
    *.pdf
  3. mkoertgen revised this gist Oct 1, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion doc2pdf.bat
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,6 @@ setlocal
    set input=%1
    if "%input%"=="" set input=*.docx

    for %%a in (%input%) do powershell -f doc2pdf.ps1 %%a %%~dpna.pdf
    for %%a in (%input%) do powershell -f doc2pdf.ps1 "%%a" "%%~dpna.pdf"
    )
    endlocal
  4. mkoertgen revised this gist Sep 24, 2015. 2 changed files with 15 additions and 1 deletion.
    9 changes: 9 additions & 0 deletions doc2pdf.bat
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    @echo off
    setlocal

    set input=%1
    if "%input%"=="" set input=*.docx

    for %%a in (%input%) do powershell -f doc2pdf.ps1 %%a %%~dpna.pdf
    )
    endlocal
    7 changes: 6 additions & 1 deletion doc2pdf.ps1
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,11 @@ param([string]$DocInput, [string]$PdfOutput = '.\output.pdf')

    Add-type -AssemblyName Microsoft.Office.Interop.Word

    #
    # - optimize for screen/print: wdExportOptimizeForOnScreen / wdExportOptimizeForPrint
    # - content only/include markups & comments: wdExportDocumentContent / wdExportDocumentWithMarkup
    # - create bookmarks: wdExportCreateHeadingBookmarks / wdExportCreateWordBookmarks

    function WordToPdf([string]$wdSourceFile, [string]$wdExportFile) {
    $wdExportFormat = [Microsoft.Office.Interop.Word.WdExportFormat]::wdExportFormatPDF
    $wdOpenAfterExport = $false
    @@ -17,7 +22,7 @@ function WordToPdf([string]$wdSourceFile, [string]$wdExportFile) {
    $wdExportItem = [Microsoft.Office.Interop.Word.WdExportItem]::wdExportDocumentContent
    $wdIncludeDocProps = $true
    $wdKeepIRM = $true
    $wdCreateBookmarks = [Microsoft.Office.Interop.Word.WdExportCreateBookmarks]::wdExportCreateWordBookmarks
    $wdCreateBookmarks = [Microsoft.Office.Interop.Word.WdExportCreateBookmarks]::wdExportCreateHeadingBookmarks
    $wdDocStructureTags = $true
    $wdBitmapMissingFonts = $true
    $wdUseISO19005_1 = $false
  5. mkoertgen created this gist Oct 27, 2014.
    81 changes: 81 additions & 0 deletions doc2pdf.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    # cf.:
    # - http://blog.coolorange.com/2012/04/20/export-word-to-pdf-using-powershell/
    # - https://gallery.technet.microsoft.com/office/Script-to-convert-Word-f702844d
    # http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/24/weekend-scripter-convert-word-documents-to-pdf-files-with-powershell.aspx

    param([string]$DocInput, [string]$PdfOutput = '.\output.pdf')

    Add-type -AssemblyName Microsoft.Office.Interop.Word

    function WordToPdf([string]$wdSourceFile, [string]$wdExportFile) {
    $wdExportFormat = [Microsoft.Office.Interop.Word.WdExportFormat]::wdExportFormatPDF
    $wdOpenAfterExport = $false
    $wdExportOptimizeFor = [Microsoft.Office.Interop.Word.WdExportOptimizeFor]::wdExportOptimizeForOnScreen
    $wdExportRange = [Microsoft.Office.Interop.Word.WdExportRange]::wdExportAllDocument
    $wdStartPage = 0
    $wdEndPage = 0
    $wdExportItem = [Microsoft.Office.Interop.Word.WdExportItem]::wdExportDocumentContent
    $wdIncludeDocProps = $true
    $wdKeepIRM = $true
    $wdCreateBookmarks = [Microsoft.Office.Interop.Word.WdExportCreateBookmarks]::wdExportCreateWordBookmarks
    $wdDocStructureTags = $true
    $wdBitmapMissingFonts = $true
    $wdUseISO19005_1 = $false

    $wdApplication = $null;
    $wdDocument = $null;

    # How to: Programmatically Close Documents (without changes)
    # http://msdn.microsoft.com/en-us/library/af6z0wa2.aspx
    $doNotSaveChanges = [Microsoft.Office.Interop.Word.WdSaveOptions]::wdDoNotSaveChanges

    try
    {
    $wdApplication = New-Object -ComObject "Word.Application"
    $wdDocument = $wdApplication.Documents.Open($wdSourceFile)
    $wdDocument.ExportAsFixedFormat(
    $wdExportFile,
    $wdExportFormat,
    $wdOpenAfterExport,
    $wdExportOptimizeFor,
    $wdExportRange,
    $wdStartPage,
    $wdEndPage,
    $wdExportItem,
    $wdIncludeDocProps,
    $wdKeepIRM,
    $wdCreateBookmarks,
    $wdDocStructureTags,
    $wdBitmapMissingFonts,
    $wdUseISO19005_1
    )
    }
    catch
    {
    $wshShell = New-Object -ComObject WScript.Shell
    $wshShell.Popup($_.Exception.ToString(), 0, "Error", 0)
    $wshShell = $null
    }
    finally
    {
    if ($wdDocument)
    {
    $wdDocument.Close([ref]$doNotSaveChanges)
    $wdDocument = $null
    }
    if ($wdApplication)
    {
    $wdApplication.Quit()
    $wdApplication = $null
    }
    [GC]::Collect()
    [GC]::WaitForPendingFinalizers()
    }
    }

    $FullInput = (Get-Item ${DocInput}).FullName
    # http://stackoverflow.com/questions/3038337/powershell-resolve-path-that-might-not-exist
    $FullOutput = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(${PdfOutput})

    Write-Host "Converting ${FullInput} to ${FullOutput}..."
    WordToPdf ${FullInput} ${FullOutput}