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.

9
  • 1
    can you able to add more information about your question Commented Feb 2, 2018 at 5:35
  • by using pd4ml and itext pdf you can convert HTML page into pdf or you can make your own pdf Commented Feb 2, 2018 at 5:36
  • do you mean, you want to save the current web page as pdf? Commented Feb 2, 2018 at 5:47
  • stackoverflow.com/questions/34049956/… Commented Feb 3, 2018 at 11:43
  • have you considered using Puppeteer from Google? developers.google.com/web/tools/puppeteer/?authuser=3&hl=vi Commented Feb 5, 2018 at 15:23

2 Answers 2

3
+25

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>
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.