Shark.PdfConvert 1.0.5

.NET Standard 1.6 .NET Framework 4.5.1
NuGet\Install-Package Shark.PdfConvert -Version 1.0.5
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.
dotnet add package Shark.PdfConvert --version 1.0.5
<PackageReference Include="Shark.PdfConvert" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Shark.PdfConvert --version 1.0.5
#r "nuget: Shark.PdfConvert, 1.0.5"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Shark.PdfConvert as a Cake Addin
#addin nuget:?package=Shark.PdfConvert&version=1.0.5

// Install Shark.PdfConvert as a Cake Tool
#tool nuget:?package=Shark.PdfConvert&version=1.0.5


What is Shark.PdfConvert?

Shark.PdfConvert is a simple .NET Core (also targets net451) wrapper around the WkHtmlToPdf tool. Most options are exposed via a PdfConversionSettings object, others can be specified by using Custom overrides for the configuration area you want.

Conversion setting defaults are set for a Windows environment and assume you have the WkHTMLToPDF (x64) tool installed. You can override the Path to the tool by overridding PdfConversionSettings . PdfToolPath.

You will need to install/download WkHtmlToPdf, it is not embedded in the NuGet Package

Sample 1: Static HTML Content

PdfConvert.Convert(new PdfConversionSettings
    Title = "My Static Content",
    Content = @"<h1>Lorem ipsum dolor sit amet consectetuer adipiscing elit 
		<script>document.querySelector('h1').style.color = 'rgb(128,0,0)';</script>",
    OutputPath = @"C:\temp\temp.pdf"

Sample 2: Get Content from a URL

PdfConvert.Convert(new PdfConversionSettings
    Title = "My URL based Content",
    ContentUrl = "",
    OutputPath = @"C:\temp\temp-url.pdf"

Sample 3: Use Streams for Output and Input

PdfConversionSettings config = new PdfConversionSettings
    Title = "Streaming my HTML to PDF"

using (var fileStream = new FileStream(Path.GetTempFileName() + ".pdf", FileMode.Create))
    var task = new System.Net.Http.HttpClient().GetStreamAsync("");

    using (var inputStream = task.Result)
		PdfConvert.Convert(config, fileStream, inputStream);

Sample 4: Mix and Match

PdfConversionSettings config = new PdfConversionSettings
    Title = "A little bit of Everything",
    GenerateToc = true,
    TocHeaderText = "Table of MY Contents",
    PageCoverUrl = "",
    ContentUrl = "",
    PageHeaderHtml = @"
        <!DOCTYPE html>
        <div style=""background-color: red; color: white; text-align: center; width: 100vw;"">SECRET SAUCE</div>

using (var fileStream = new FileStream(Path.GetTempFileName() + ".pdf", FileMode.Create))
    PdfConvert.Convert(config, fileStream);

Sample 5: Usage inside MVC Controller Action

public IActionResult ConvertToPdf([FromBody] PdfConversionSettings model) 
	// TAKE CARE WHEN Accepting the Conversion Settings from user land, it would be best 
	// to just NOT DO it, accept your own custom model and map the parameters as needed.
	// If you insist, then you could do something like the following to prevent malicious code execution
	// in my testing the Custom*Args members are not a valid attack vector, PdfToolPath certainly is, never* trust
	// the client
    // set path to executable, UNSAFE DEBUG USE ONLY FOR TESTING
    model.PdfToolPath = model.PdfToolPath ?? _host.ContentRootPath + @"\wkhtmltopdf.exe";
    // set path to executable
    model.PdfToolPath = _host.ContentRootPath + @"\wkhtmltopdf.exe";

    if (model.OutputFilename.EndsWith(".pdf") == false) model.OutputFilename = model.OutputFilename + ".pdf";

    var memoryStream = new MemoryStream();
    PdfConvert.Convert(model, memoryStream);
    return new FileContentResult(memoryStream.ToArray(), MimeTypes.Pdf)
        FileDownloadName = model.OutputFileName

Sample 6: Get Content from multiple URLs

var settings = new PdfConversionSettings
    Title = "My Content from multiple URLs",
    OutputPath = @"C:\temp\temp-url-multiple.pdf"


Sample 7: Zoom and Page Size (Issue #5)

PdfConvert.Convert(new PdfConversionSettings
    Title = "Converted by Shark.PdfConvert",
    LowQuality = false,
    Margins = new PdfPageMargins() { Bottom = 10, Left = 10, Right = 10, Top = 10 },
    Size = PdfPageSize.A3,
    Zoom = 3.2f,
    Content = @"<h1>Lorem ipsum dolor sit amet consectetuer adipiscing elit I SHOULD BE RED BY JAVASCRIPT</h1><script>document.querySelector('h1').style.color = 'rgb(128,0,0)';</script>",
    OutputPath = @"C:\temp\sample7.pdf"

Revision History*

  • 1.0.4 - Merged PRs from very patient PR submitters #16, #9, and #7
  • 1.0.3 - Fixed Issue #5 with Zoom / Page Size options and Fixed Header/Footer/Cover issues.
  • 1.0.2 - Added ContentUrls property to PdfConversionSettings to allow for multiple URLs to be specified. Requested via Issue.
  • 1.0.1 - Spoke to soon, updated the samples, they had a typo, small tweaks in the code, nothing breaking or signature modifying
  • 1.0.0 - Should be stable going forward except for any bugs found. Modified Convert method signature to be a bit more sane, Added additional static content options, Added Url overrides if you wanted to have WkHTMLToPDF grab external sites for any portion of the generated document, exposed some process options
  • 0.1.0 - Initial Upload
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp1.0 netcoreapp1.1 netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard1.6 netstandard2.0 netstandard2.1
.NET Framework net451 net452 net46 net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen30 tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.5 30,963 11/6/2020
1.0.4 2,546 9/24/2020
1.0.3 54,870 6/7/2018
1.0.2 785 6/5/2018
1.0.1 3,461 2/21/2017

Version 1.0.5 - --quiet option
Version 1.0.4 - Merged PRs from very patient PR submitters
Version 1.0.3 - Fixing issues 2 and 5 (Zoom/Page/Header/Footer/Cover)
Version 1.0.2 - Added ContentUrls property to PdfConversionSettings to allow specifying multiple URLs
Version 1.0.1 - Fixed some minor issues with the tests