Coding

Export HTML to PDF in Dot Net Core

April 18, 2017

In the past I have wrote an article on how to export html to a pdf in dot net mvc (non dot net core), I have since needed to achieve the same result but using dot net core. The previous article was http://neil.red/coding/c-how-to-export-html-to-pdf-for-free

The solution I have found for dot net core was to use nodejs and seems a rather painless solution.

First you need to install the Microsoft.AspNetCore.NodeServices package

Then in your Startup add this line services.AddNodeServices() like this

Now you need to install the required NodeJs packages, if you already have a package.json you can simply add to it:

Or you can install from the command line like

Then add the packages

Then in your root folder e.g. where your .xproj file is not the wwwroot folder in dot net core, add a new file called pdf.js and put in it

Then you need to create a method in a controller to call this nodejs file

This simply says, when you hit the route of /exportpdf, then call the nodejs service ./pdf (pdf.js in the root) and pass in a string of data “the data from a controller”.

The pdf.js will then take that data and use jsreport to convert the html into pdf data to be sent back to the method and returned as a ContentResult with type of application/pdf.

This is only a simple example, but you would be able to pass in all sorts of html string in the place of “the data from a controller” parameter and have the nodejs service convert that html to pdf.

You Might Also Like

8 Comments

  • Reply Aaron October 12, 2017 at 6:14 pm

    Did you ever get the following error:
    Call to Node module failed with error: Error: spawn …node_modules\\phantomjs\\lib\\phantom\\phantomjs.exe ENOENT

    • Reply Neil Redfern November 17, 2017 at 12:21 am

      No I never got that. I’ve had a quick look and it from this post getting ENOENT in node_modules, it sounds like it could be a permissions error: https://github.com/npm/npm/issues/3664

      Perhaps check folder permissions for node_modules or even a fresh npm install?

      • Reply Neil Redfern November 21, 2017 at 7:23 pm

        Just to add to this.. i recently did get this error message of “ENOENT” for something else and the error was a folder permissions problem. It depends on what OS you are running etc how you need to set folder permissions though.

  • Reply Marcel November 21, 2017 at 10:56 pm

    Yes, this works, but how can i include an image is ignored in the output

    • Reply Neil Redfern November 21, 2017 at 11:14 pm

      I think you may need to convert the images from relative paths to absolute urls in the html you pass into the pdf.

  • Reply chetna March 23, 2018 at 6:32 am

    how to set header and footer in this above node services.

    • Reply Neil Redfern March 23, 2018 at 7:53 am

      I haven’t tested this.. but I believe you can put inside the template object in the pdf.js two new properties of: header and footer.. e.g.

      template: {
      content: ‘

      {{:foo}}

      ‘,
      engine: ‘jsrender’,
      recipe: ‘phantom-pdf’,
      header: ‘header content’,
      footer: ‘footer content’
      },

    • Reply Shalin July 25, 2018 at 1:34 pm

      Did you find a way to set Header , Footer , Paging ? Please share a sample 🙂

    Leave a Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.