VOOZH about

URL: https://www.nuget.org/packages/Sats.Core.HTMLToPdf/

⇱ NuGet Gallery | Sats.Core.HTMLToPdf 2.0.0




Sats.Core.HTMLToPdf 2.0.0

dotnet add package Sats.Core.HTMLToPdf --version 2.0.0
 
 
NuGet\Install-Package Sats.Core.HTMLToPdf -Version 2.0.0
 
 
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sats.Core.HTMLToPdf" Version="2.0.0" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sats.Core.HTMLToPdf" Version="2.0.0" />
 
Directory.Packages.props
<PackageReference Include="Sats.Core.HTMLToPdf" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Sats.Core.HTMLToPdf --version 2.0.0
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Sats.Core.HTMLToPdf, 2.0.0"
 
 
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Sats.Core.HTMLToPdf@2.0.0
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Sats.Core.HTMLToPdf&version=2.0.0
 
Install as a Cake Addin
#tool nuget:?package=Sats.Core.HTMLToPdf&version=2.0.0
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

HTML to PDF Conversion using Chrome Executable

This package allows you to convert HTML files to PDF using the Chrome executable. It leverages Chrome's headless mode to generate PDFs from local or remote HTML files efficiently.

Features

  • Headless Chrome Mode: Converts HTML to PDF without launching a full browser window.
  • Custom Output: Allows custom PDF file output locations.
  • No Header/Footer: Removes the default header and footer from the PDF.
  • Option for User Directory: Allows the use of a custom user directory for the conversion process.

Installation

To install this package, follow these steps:

  1. Download the latest .nupkg file from the GitHub Releases page, or use NuGet Package Manager in Visual Studio or the .NET CLI:

    dotnet add package HtmlToPdf --version 2.0.0
    
  2. Include the Required Dependencies:

    • Ensure that Google Chrome is installed on your system.
    • Set the correct paths for the HTML file and Chrome executable in your application.

Usage

Below is an example of how to use the package to convert an HTML file to PDF.

using System;
using System.IO;

class Program
{
 static void Main(string[] args)
 {
 // Path to the HTML file you want to convert
 var url = @"d:\3bc00512-c01a-4f6d-a26e-77616ef51807.html";

 // Path to your Chrome executable
 var chromePath = @"C:\Program Files\Google\Chrome\Application\chrome.exe";

 // Use the ChromeOptions class to set conversion options
 var output = new ChromeOptions().AddOptions(b =>
 {
 // Set Chrome to headless mode (no UI)
 b.Headless();

 // Disable GPU (optional)
 b.DisableGPU();

 // Remove header and footer from the generated PDF
 b.WithoutHeader();
 }).ToPdf(new ChromeDetails()
 {
 ChromePath = chromePath, // Path to Chrome executable
 HtmlPath = url, // Path to HTML file
 DeleteOutputFile = true, // Delete temporary output file after conversion (optional)
 UserDirectoryPath = "d:\\userdir" // Path for Chrome's user data directory (optional)
 });

 // Save the generated PDF to the specified path
 File.WriteAllBytes(@"d:\print.pdf", output.FileDetails.File);
 }
}

Parameters Explained:

  • HtmlPath: The file path of the HTML document you want to convert to PDF.
    • Example: "d:\\3bc00512-c01a-4f6d-a26e-77616ef51807.html"
  • ChromePath: The file path to your Google Chrome executable.
    • Example: "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
  • DeleteOutputFile: A boolean flag to specify whether the temporary output file (during the conversion process) should be deleted.
    • Default: true
  • UserDirectoryPath: Optional path where Chrome will store user data during the conversion process (used for handling cookies, cache, etc.).
    • Example: "d:\\userdir"

Customization Options:

  • Headless Mode: The Headless() option allows Chrome to run in headless mode, meaning no graphical user interface (GUI) is shown.

  • Disable GPU: The DisableGPU() option can be used to disable the GPU hardware acceleration.

  • Remove Header/Footer: The WithoutHeader() option removes any page numbers, date, or URL that Chrome normally adds to the PDF.

Example Output

  • The HTML content at the specified path will be converted to a PDF and saved at the path d:\print.pdf.
  • Any headers and footers (like page numbers and dates) will be removed.

Common Issues and Troubleshooting

  • Issue: No PDF Generated

    • Ensure Chrome is installed and the path is correctly specified in the ChromePath property.
    • Make sure the HTML file path is correct and accessible.
  • Issue: Header/Footer Still Appears

    • Ensure that you are using the WithoutHeader() option. If the problem persists, check if any default settings in Chrome override this option.
  • Issue: Permissions Error

    • Ensure the paths you are providing for HtmlPath and output files (d:\print.pdf) are accessible and writable.

License

This package is licensed under the MIT License.


Contributors

  • satsvelke (author)

Icons:

  • 📄 HTML File: Used as the input file.
  • 🚀 Chrome Executable: Used to run the headless Chrome process.
  • 🖨️ PDF Output: The final generated PDF file.

If you encounter any issues or have questions, feel free to open an issue on the GitHub repository.

Product Versions Compatible and additional computed target framework versions.
.NET net5.0 net5.0 is compatible.  net5.0-windows net5.0-windows was computed.  net6.0 net6.0 is compatible.  net6.0-android net6.0-android was computed.  net6.0-ios net6.0-ios was computed.  net6.0-maccatalyst net6.0-maccatalyst was computed.  net6.0-macos net6.0-macos was computed.  net6.0-tvos net6.0-tvos was computed.  net6.0-windows net6.0-windows was computed.  net7.0 net7.0 is compatible.  net7.0-android net7.0-android was computed.  net7.0-ios net7.0-ios was computed.  net7.0-maccatalyst net7.0-maccatalyst was computed.  net7.0-macos net7.0-macos was computed.  net7.0-tvos net7.0-tvos was computed.  net7.0-windows net7.0-windows was computed.  net8.0 net8.0 is compatible.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 is compatible.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 was computed.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
.NET Core netcoreapp3.1 netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.1

    • No dependencies.
  • net5.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Sats.Core.HTMLToPdf:

Package Downloads
Meddata.LibraryHelpers

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 244 12/20/2024
1.0.0 1,149 10/2/2021

Usage
     var url = @"d:\convert.html";
     var chromePath = @"C:\Program Files\Google\Chrome\Application\chrome.exe";

     var output = new ChromeOptions().AddOptions(b =>
     {
     b.Headless();
     b.DisableGPU();
     b.WithoutHeader();

     }).ToPdf(new ChromeDetails()
     {
     ChromePath = chromePath,
     HtmlPath = url,
     DeleteOutputFile = true, //optional
     // OutputPath = @"d:\print.pdf" // (add if Environment.CurrentDirectory does not have access rights)
     });
     File.WriteAllBytes(@"d:\print.pdf", output.FileDetails.File);