HTML to PDF, but on Windows

This follows from my previous post about using selenium webdriver to print a pdf of html. Turns out that selenium is available for .NET/C# which means we can load it from powershell. Convert HTML to PDF

Head to the download page for selenium and choose .NET/C#. It opens the nuget.org package page. I'm not actually sure how you are supposed to install nupkgs so I downloaded the .nupkg file from "Download Package" on the right side. Open the .nupkg with 7-zip and extract lib/net8.0/WebDriver.dll and runtimes/win/native/selenium-manager.exe These should go in the same directory as the powershell script.

Run Selenium from PowerShell

We load the .net DLL with Add-Type, this makes the types available in the DLL to PowerShell. Create a new EdgeDriver. Set the appropriate print options.

Add-Type -LiteralPath .\WebDriver.dll; $driver = [OpenQA.Selenium.Edge.EdgeDriver]::new(); $po = [OpenQA.Selenium.PrintOptions]::new(); # Set to A4 portrait $po.Orientation = "Portrait"; $po.PageDimensions.Height = 29.7; $po.PageDimensions.Width = 21.0; # Margins are in the document already $po.PageMargins.Top = 0; $po.PageMargins.Bottom = 0; $po.PageMargins.Left = 0; $po.PageMargins.Right = 0; $po.ScaleFactor = 1; $po.ShrinkToFit = $false; $fullpath = Get-Item "input.html" | Select-Object -ExpandProperty FullName; $driver.url = "file://$($fullpath)"; $doc = $driver.Print($po); [System.IO.File]::WriteAllBytes("output.pdf", [System.Convert]::FromBase64String($doc.AsBase64EncodedString)); $driver.Quit();