none
Powershell: txt to pdf conversion RRS feed

  • Question

  • Am trying to convert txt file to pdf using powershell and facing with 2 issues, 1) the spacing between the headers and detail records are getting removed 2) it converts only 2 pages. Any help, please.

    Here is my code:

    # File paths
    $txtPath = "C:\Users\suresh\Desktop\TXT2PDF\Input.txt"
    $pdfPath = "C:\Users\suresh\Desktop\TXT2PDF\Output.pdf"
    $docTemplate = "C:\Users\suresh\Desktop\TXT2PDF\fd_pdf.dotx"

    # Required Word Variables
    $wdExportFormatPDF = 17
    $wdDoNotSaveChanges = 0

    # Create a hidden Word window
    $word = New-Object -ComObject word.application
    $word.visible = $false

    # Add a Word document
    $doc = $word.documents.open($docTemplate)

    # Put the text into the Word document
    $txt = Get-Content $txtPath
    $selection = $word.selection
    $selection.typeText($txt)

    # Set the page orientation to landscape
    #$doc.PageSetup.Orientation = 1


    # Export the PDF file and close without saving a Word document
    $doc.ExportAsFixedFormat($pdfPath,$wdExportFormatPDF)
    $doc.close([ref]$wdDoNotSaveChanges)
    $word.Quit() 

    Saturday, July 13, 2019 1:11 AM

All replies

  • Your code is incorrect.  You are opening a template and inserting text then trying to save the template.  You want to open the text file and add a template to that document.


    \_(ツ)_/

    Saturday, July 13, 2019 1:45 AM
  • This works best for auto-applying the template.

    $wd = New-Object -ComObject word.application
    $wd.visible = $false
    $template = $wd.documents.open($docTemplate)
    
    $template.Range().Text = Get-Content $txtPath | Out-String
    $template.ExportAsFixedFormat($pdfPath, [Microsoft.Office.Interop.Word.WdExportFormat]::wdExportFormatPDF)
    
    $template.close([Microsoft.Office.Interop.Word.WdSaveOptions]::wdDoNotSaveChanges)
    $wd.Quit()
    


    \_(ツ)_/


    • Edited by jrv Monday, July 15, 2019 9:48 PM
    Saturday, July 13, 2019 11:53 AM
  • Thank you, it worked as expected however am getting exceptions at the last 2 commands.

    You cannot call a method on a null-valued expression.
    At C:\Users\suresh\Desktop\Working_Script_txt_2_pdf.ps1:12 char:1
    + $doc.close([Microsoft.Office.Interop.Word.WdSaveOptions]::wdDoNotSave ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
     
    You cannot call a method on a null-valued expression.
    At C:\Users\suresh\Desktop\Working_Script_txt_2_pdf.ps1:13 char:1
    + $word.Quit()
    + ~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    Any insight on what does these run time errors specify?. Am learning PowerShell day by day, if you can point me to any documentation, I will try to find out. Thanks again for your time.

    Monday, July 15, 2019 9:46 PM
  • Change $word to $wd.

    Change $doc to $template.


    \_(ツ)_/


    • Edited by jrv Monday, July 15, 2019 9:49 PM
    Monday, July 15, 2019 9:48 PM