0

I have the following problem: a powershell script is to be written that sends me all files in a folder with endings such as e.g. .c .cpp .java .xml etc. into a pdf. each file should start with a new page and the file name should be at the beginning.

Add-Type -Path "C:\Program Files\WindowsPowerShell\Modules\itextsharp\5.5.13.3\lib\itextsharp.dll"

function Merge-FilesToPdf {
    param(
        [Parameter(Mandatory = $true)]
        [string]$SourcePath,
        
        [Parameter(Mandatory = $true)]
        [string]$DestinationFile
    )
    
   
    # Create a PDF document
    $document = New-Object iTextSharp.text.Document
    
    # Initialize the PDF writer
    $writer = [iTextSharp.text.pdf.PdfWriter]::GetInstance($document, [System.IO.File]::Create($DestinationFile))
    
    # Open the PDF document
    $document.Open()
    
    try {
        # Define the font
        $font = [iTextSharp.text.pdf.BaseFont]::CreateFont([iTextSharp.text.pdf.BaseFont]::COURIER, [iTextSharp.text.pdf.BaseFont]::CP1252, [iTextSharp.text.pdf.BaseFont]::EMBEDDED)
        
        # List files in the source directory
        $files = Get-ChildItem -Path $SourcePath -File
        
        foreach ($file in $files) {
            # Check if the file is a text file
            if ($file.Extension -match '\.(txt|c|log|xml|ps1)$') {
                # Create a new page
                $document.NewPage()
                
                # Add the file name as text
                $header = New-Object iTextSharp.text.Paragraph($file.Name)
                $header.Font = $font  # Set font for the file name
                $document.Add($header)
                
                # Add the file content to the PDF
                $content = Get-Content -Path $file.FullName -Raw
                $paragraph = New-Object iTextSharp.text.Paragraph($content)
                $paragraph.Font = $font  # Set font for the file content
                $document.Add($paragraph)
            }
        }
    }
    finally {
        # Close the PDF document and release memory
        $document.Close()
        $writer.Close()
        $document.Dispose()
        $writer.Dispose()
    }
}

Merge-FilesToPdf -SourcePath "C:\Projekt\test\kl01\erg001" -DestinationFile "C:\Output.pdf"



This code is finally working, it just doesn't do the formatting in the pdf files like they are in the original files. does anyone have an idea how to solve this?

Edit: I mean if I have for example a C code as a source, the code will be left justified in the PDF, tabs will not transfer

ZoniK
  • 31
  • 2
  • What do you mean, "it just doesn't do the formatting in the pdf files like they are in the original files"? What kind of "formatting" are you talking about? Is it line indentation? – lit Jun 13 '23 at 14:35
  • Sorry, I mean if I have for example a C code as a source, the code will be left justified in the PDF, tabs will not transfer – ZoniK Jun 13 '23 at 14:45
  • It might be worth running the text files containing CHARACTER TABULATION \x09 characters through something to convert them to SPACE characters. – lit Jun 13 '23 at 16:12

1 Answers1

0

PDF traditionally does not support LinePrinter/Teletype Tabs

Hence the use of fixed spaces and NON proportional Fonts such as Courier/Consolas

I had suggested using Nenscript in your previous question, as an alterative to Enscript, however on testing, there too "Tabs Support" was "Lacking".

So I propose use traditional Enscript for Windows which supports ASCII/ANSI tabs.

A typical batch file for drag and drop or looping through files could look like
enter image description here

Enscripter.cmd

set "enscript=%~dp0enscript\bin\enscript.exe"
set "gs=C:\Program Files\gs\gs10.01.1\bin\gswin64c.exe"
set "tab=4"

REM -t title here is set as filename without extension, and other switches are available

"%enscript%" -B -t"%~n1" -T%tab% "%~dpn1.txt" -p"%temp%\tmp.ps"
"%gs%" -sDEVICE=pdfwrite -o"%~dpn1.pdf" -f "%temp%\tmp.ps"

NOTE this is bare minimal example to allow for "Tabs", and does NOT itself embed a Courier Font which is better done within the GS command, but that's a wider question with several answers.

If you do not have the 3 critical files (1 exe & 2 dll's) use this install/unpacker.
instEnscript.cmd

md installer
cd installer
curl -o innounp.zip https://packages.chocolatey.org/innounp.0.50.nupkg
if not exist innounp.zip timeout 3
tar -xf innounp.zip tools/innounp.exe
if not exist tools\innounp.exe timeout 2
move tools\innounp.exe innounp.exe
timeout 2 &&if not exist innounp.exe echo FAILED Phase 1 && pause
rd tools
curl -O "https://master.dl.sourceforge.net/project/gnuwin32/enscript/1.6.3-9/enscript-1.6.3-9-bin.exe?viasf=1"
timeout 3&&if not exist enscript-1.6.3-9-bin.exe echo FAILED Phase 2 && pause
innounp -x enscript-1.6.3-9-bin.exe
timeout 4 &&if not exist {app}\bin\enscript.exe echo FAILED Phase 3 && pause
if not exist {app}\bin\libiconv-2.dll echo FAILED Phase 3 && pause
if not exist {app}\bin\libintl-2.dll echo FAILED Phase 3 && pause
move {app} .. &&cd ..\{app}
curl -O "https://master.dl.sourceforge.net/project/gnuwin32/enscript/1.6.3-9/enscript-1.6.3-9-doc.zip?viasf=1"
timeout 2 &&tar -xf enscript-1.6.3-9-doc.zip
timeout 2 &&cd ..
move {app}\enscript-1.6.3-9-doc.zip installer
move {app} enscript

K J
  • 8,045
  • 3
  • 14
  • 36