3

I want to create a PDF file so that when I click on a button SaveToPDF it should fulfill the requirement. But I'm unable to do it, any help on how to achieve this using RestEndCall through back-end using SpringMVC and for front-end using Angular4/5/6/7/8+

Also, what data should I be sending to back-end? And to be precise, I don't want to create each cell by cell. If there is any other option to create a PDF which look exactly like the web page.

Bhushan
  • 104
  • 1
  • 9
user3132347
  • 363
  • 1
  • 7
  • 27

2 Answers2

3

You can easily achieve it in the frontend, no need to backed request. Here is angular example:

<html ng-app="app">
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.22/pdfmake.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
</head>
<script>
var app = angular.module("app", []);

 app.controller("listController", ["$scope",
   function($scope) {
     $scope.data=  [{"agence":"CTM","secteur":"Safi","statutImp":"operationnel"}];

     $scope.export = function(){
        html2canvas(document.getElementById('exportthis'), {
            onrendered: function (canvas) {
                var data = canvas.toDataURL();
                var docDefinition = {
                    content: [{
                        image: data,
                        width: 500,
                    }]
                };
                pdfMake.createPdf(docDefinition).download("test.pdf");
            }
        });
     }
   }
 ]);
</script>
</head>
<body>
  <div ng-controller="listController">
    <div id="exportthis">
      download me as a pdf
    </div>
    <button ng-click="export()">export</button>
  </div>
<body>
</html>
Bhushan Uniyal
  • 5,575
  • 2
  • 22
  • 45
2

You ask about how to create a Page screenshot in a PDF format.

In practice, client-side libraries for making pdf are very limited by functionality and produce poor result,

Whats left is server-side rendering.

I had success with wkhtmltopdf a long time ago, there are some other alternatives.

You can start your implementation journey by searching wkhtmltopdf or PhantomJS alternatives for Java.

How should this work? Headless browsers open a URL and wait for a window.variable, when true script (same wkhtmltopdf for example) makes the job. There is an answer with a list of headless browsers. Best PDF solutions will be based on any of them.

Roman M. Koss
  • 970
  • 1
  • 15
  • 33
  • This really is the best answer, we are currently using wkhtmltopdf and while it doesn't always get the conversion perfectly, it really is the best we have found. – init fail Feb 08 '18 at 17:34