How can I print to PDF from an html document?

I would like to know if anyone can help me with the following:

I need to print in PDF format from an html document (it is being done for mobile device and for desktop). With Phonegap or cordova and ionic

The one I'm going to show here is by desktop with electron

This is the code I'm testing:

$scope.generar_pdf = function(){

           html = $compile(angular.element('#cuerpocorreo'))($scope);
           print_win = new BrowserWindow();
           print_win.loadURL("data:text/html;charset=utf-8," + encodeURI(html[0].innerHTML));


           print_win.webContents.on('did-finish-load', function() {
                print_win.webContents.print(); 
            });
           print_win.on('closed', function() {
                print_win = null;
            });
           $scope.borrar_historial();      
    }

This is how it should look:

What comes out when implementing the above code:

In advance I thank the help that they can give me.

 3
Author: Andrea, 2016-02-02

2 answers

An alternative could be to use mPDF, which I used.

I have here an example of how to implement it using PHP.

Update:

Searching the internet I found this similar question in English: https://stackoverflow.com/questions/9818093/how-to-print-html-to-pdf

Where they suggest another 2 options:

 1
Author: Jorgesys, 2017-05-23 12:39:20

A good option would be to use nodejs because it is cross-platform and works even on one device Android as you can see here

Then you have the package html-pdf that you could use to generate the PDF

 0
Author: rsciriano, 2016-02-04 23:18:49