Skip to content

Files

Failed to load latest commit information.

Latest commit

 Cannot retrieve latest commit at this time.

History

History

ForEvolve.Pdf

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

ForEvolve.Pdf

This library contains implementations of the ForEvolve.Pdf.Abstractions.IHtmlToPdfConverter interface, allowing convertion of HTML to a PDF.

For now, there is only one implementation, using PhantomJs.

PhantomJs

To use the PhantomJs implementation (the only one at the moment), you need to reference the ForEvolve.PhantomJs.Dependencies package.

That package contains the PhantomJS binaries for Windows, MacOS, and Linux and it has a significant size (more than 100mb).

How to use it

In Startup.cs, register the dependencies:

services.AddForEvolvePhantomJsHtmlToPdfConverter();

Then, wherever you want in your code, inject the IHtmlToPdfConverter service.

Example:

public class SomeService
{
    private readonly IHtmlToPdfConverter _htmlToPdfConverter;

    public SomeService(IHtmlToPdfConverter htmlToPdfConverter)
    {
        _htmlToPdfConverter = htmlToPdfConverter ?? throw new ArgumentNullException(nameof(htmlToPdfConverter));
    }

    //...
}

Now to use it, you can:

var targetDirectory = "C:\\my-pdf";
var htmlToConvert = @"
<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <h1>Hello World!</h1>
    <p>This PDF has been generated by PhantomJs ;)</p>
</body>
</html>
";
var pathOftheGeneratedPdf = HtmlToPdfConverter.Convert(htmlToConvert, targetDirectory);

Finally, you can do what you want with the pdf file; you have the file name in the pathOftheGeneratedPdf variable.

How to use styles

Since the implementation uses PhantomJS, a headless browser, you can use CSS styles or load stylesheets.

Example:

<head>
    <style>
        body {
            color: #333;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        h1 {
            background: #ff0000;
        }
    </style>
</head>

or even:

<head>
    <link href=""../../assets/css/pdf-styles.css"" rel=""stylesheet"" />
</head>

In this case, assuming you are using the default PhantomJS directory, the pdf-styles.css file must reside in the $(OutDir)\assets\css directory. Example: bin\Debug\netcoreapp2.1\assets\css.

What you can do is to CopyToOutputDirectory=PreserveNewest them in your csproj file.

Example:

<ItemGroup>
    <None Update="assets\css\pdf-styles.css">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
</ItemGroup>

Special thanks

The idea (and the initial code) is from https://github.com/TheSalarKhan/PhantomJs.NetCore. I was searching and experimenting with ways to generate PDFs from .Net Core and that was the one I choose.

After a little rewrite and a 100% code coverage result (plus some end to end tests that I will publish as a sample application when I find the time), I was happy enough with it and decided to release the library in my framework.