Introduction
In this article, I am going to explain how to generate PDF file from an HTML page using AngularJS in SharePoint. This is the effortless way to port the SharePoint list data or any other list data to carry, print, and make a hard copy. Unless we generate PDF files with the SharePoint list data, we can’t take it out of a SharePoint Server to manipulate the data physically. This is useful when we don’t work with systems, such as when we are offline, we get power cuts, or facing internet issues.
Overview of the process
- Create a web page to show the desired data.
- Get data from SharePoint to download as pdf.
- Prebuild and design your PDFfile.
Step1
The following is the data from my SharePoint list which I would like to convert into a PDF file.
Get the desired above data from SharePoint list and display in the web page, as below.
This is how we display the data in a web page. And, the same way, we are going to make it as a PDF file.
HTML code for the above UI
Here is the HTML code for the look and feel of the above webpage.
- <body ng-app="MyApp" ng-controller="MyController">
- <div class="container-fluid">
- <div class="form-horizontal col-lg-12 col-md-12 col-sm-12 col-xs-12"> <br/>
- <div id="HideAftrSuccess">
- <div id="HeaderTop" class="col-lg-12 col-md-12 col-sm-12 col-xs-12 form-group">
- <h3 class="DivPad"><strong>Requisition Form</strong></h3>
- </div>
- <div class="form-group col-lg-12 col-md-12 col-sm-12 col-xs-12 Specifications">Order Information </div>
- <div class="form-group"> <label class="col-lg-3 col-md-3 col-sm-3 col-xs-3">Order From:</label>
- <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3"> <input class="form-control" value={{OrderFrom}} type="text" readonly/>
- <!--<p>{{getRandomSpan}}</p>-->
- </div> <label class="col-lg-1 col-md-1 col-sm-1 col-xs-1"></label> <label class="col-lg-2 col-md-2 col-sm-2 col-xs-2">PO Number:</label>
- <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3"> <input class="form-control" value={{PONumber}} type="text" readonly/> </div>
- </div>
- <div class="form-group"> <label class="col-lg-3 col-md-3 col-sm-3 col-xs-3">Confirmed To:</label>
- <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3"> <input class="form-control" value={{ConfirmedTo}} type="text" readonly/> </div> <label class="col-lg-1 col-md-1 col-sm-1 col-xs-1"></label> <label class="col-lg-2 col-md-2 col-sm-2 col-xs-2">Facility:</label>
- <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3"> <input class="form-control" value={{Facility}} type="text" readonly/> </div>
- </div>
- <div class="form-group"> <label class="col-lg-3 col-md-3 col-sm-3 col-xs-3">Date Required:</label>
- <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3"> <input class="form-control" value={{DateRequired|date}} type="text" readonly/> </div> <label class="col-lg-1 col-md-1 col-sm-1 col-xs-1"></label> <label class="col-lg-2 col-md-2 col-sm-2 col-xs-2">Ship Via:</label>
- <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3"> <input class="form-control" value={{ShipVia}} type="text" readonly/> </div>
- </div>
- <div class="form-group"> <label class="col-lg-3 col-md-3 col-sm-3 col-xs-3">Deliver To:</label>
- <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3"> <input class="form-control" value={{DeliverTo}} name="DeliverTo" type="text" readonly/> </div> <label class="col-lg-1 col-md-1 col-sm-1 col-xs-1"></label> <label class="col-lg-2 col-md-2 col-sm-2 col-xs-2">Requisition Date:</label>
- <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3"> <input class="form-control" name="RequisitionDate" value={{RequisitionDate|date}} type="text" readonly/> </div>
- </div>
- <div class="form-group"> <label class="col-lg-3 col-md-3 col-sm-3 col-xs-3">Requisitioned by:</label>
- <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
- <p>{{RequisitionedBy}}</p>
- </div> <label class="col-lg-1 col-md-1 col-sm-1 col-xs-1"></label> <label class="col-lg-2 col-md-2 col-sm-2 col-xs-2">Approver:</label>
- <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
- <p>-</p>
- </div>
- </div>
- <div class="form-group"> <label class="col-lg-3 col-md-3 col-sm-3 col-xs-3">Material For(Please Explain):</label>
- <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9"> <textarea class="form-control" readonly>{{MaterialFor}}</textarea> </div>
- </div>
- <hr id="HrLine"> </hr>
- <div class="form-group">
- <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8"></div>
- <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4"><button type="button" ng-click="savePdfSample()" class="btn btn-warning">Download pdf</button></div>
- </div>
- </div>
- </div>
- </div>
- </body>
Step 2
Script code for retrieving the data from SharePoint list
This is the script code for retrieving the desired data from the SharePoint list using Angular with REST call.
- $.ajax({
- url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('RequisitionOrders')/Items(" + GetUrlKeyValue("itemid") + ")?$select=*&$expand=RequisitionedBy&$select=RequisitionedBy/Title,RequisitionedBy/ID",
- method: "GET",
- async: false,
- headers: {
- "Accept": "application/json;odata=verbose"
- },
- success: function(data) {
-
- $(data.d).each(function(cos, currentRow) {
- $scope.OrderFromId = currentRow.ID;
- $scope.OrderFrom = currentRow.OrderFrom;
- $scope.PONumber = currentRow.PONumber;
- $scope.ConfirmedTo = currentRow.ConfirmedTo;
- $scope.Facility = currentRow.Facility;
- $scope.DateRequired = currentRow.DateRequired;
- $scope.ShipVia = currentRow.ShipVia;
- $scope.RequisitionedBy = currentRow.RequisitionedBy.Title;
- $scope.DeliverTo = currentRow.DeliverTo;
- $scope.RequisitionDate = currentRow.RequisitionDate;
- $scope.MaterialFor = currentRow.MaterialFor;
- })
- },
- error: function(sender, args) {
- console.log(args.get_message());
- }
- });
Step3
- Prebuild and design your pdf file as you desired.
- Keep ready the PDF design and hide it until you download.
- Click on “Download PDF” button. It generates the pdf file as below.
HTML code for the above PDF
This is the HTML code for the above look and feel. Here, the data is retrieved from the SharePoint list which we wanted to generate as a PDF file.
- <div id="PdfDiv" style="display:none;">
- <div style="width:594px;">
- <div><img style="text-align: left;height:20;width:90" src="/teams/mahipal/SiteAssets/Billing Form/Images/samDongLogo.jpg" /><strong style="text-align:right;border:1px solid;margin-left:350px;padding: 7px 42px">SDO</strong></div>
- <div style="text-align: right;padding-right: 18px;">
- <h3 style="text-align: center;font-family: arial;"><strong>REQUISITION</strong></h3>
- </div>
- <!-- <div style="background-color: #072a60;color: beige;padding-top: 2px;padding-bottom: 2px;">Order Information </div-->
- <div class="form-group">
- <table style="width:100%">
- <tr>
- <td style="width:20%"><span style="font-weight:700;">Order From:</span></td>
- <td style="width:20%"><label>{{OrderFrom}}</label></td>
- <td style="width:20%"><label style="font-weight:700;">PO Number:</label></td>
- <td style="width:20%"><label>{{PONumber}}</label></td>
- </tr>
- <tr>
- <td style="width:20%"><label style="font-weight:700">Confirmed To:</label></td>
- <td style="width:20%"><label>{{ConfirmedTo}}</label></td>
- <td style="width:20%"><label style="font-weight:700">Facility:</label></td>
- <td style="width:20%"><label>{{Facility}}</label></td>
- </tr>
- <tr>
- <td style="width:20%"><label style="font-weight:700">Date Required:</label></td>
- <td style="width:20%"><label>{{DateRequired|date}}</label></td>
- <td style="width:20%"><label style="font-weight:700">Ship Via:</label></td>
- <td style="width:20%"><label>{{ShipVia}}</label></td>
- </tr>
- <tr>
- <td style="width:20%"><label style="font-weight:700">Deliver To:</label></td>
- <td style="width:20%"><label>{{DeliverTo}}</label></td>
- <td style="width:20%"><label style="font-weight:700">Requisition Date:</label></td>
- <td style="width:20%"><label>{{RequisitionDate|date}}</label></td>
- </tr>
- <tr>
- <td style="width:20%"><label style="font-weight:700">Requisitioned by:</label></td>
- <td style="width:20%"><label>{{RequisitionedBy}}</label></td>
- <td style="width:20%"><label style="font-weight:700">Approver:</label></td>
- <td style="width:20%"><label>-</label></td>
- </tr>
- </table>
- </div>
- <!--<div style="background-color: #072a60;color: beige;padding-top: 2px;padding-bottom: 2px;">Item Information </div--><input style="background-color:#ffffff;border-color:#ffffff" type="button" ng-click="addReservation()" />
- <!--hr></hr-->
- <div class="form-group"> <label style="font-weight:700">Material For:</label> <label style="width:594px;">{{MaterialFor}}</label> </div>
- <!--hr></hr-->
- </div>
- </div>
- The HTML code of the PDF file is hidden by using “style= display:none” tag.
- Please use the inline style tags to build your PDF file as above.
Code for “Download PDF” button
- $scope.savePdfSample = function() {
- var pdf = new jsPDF('p', 'pt', 'letter');
- var canvas = pdf.canvas
- canvas.height = 72 * 15;
- canvas.width = 72 * 15;
- var html = $("#PdfDiv").html();
- var options = {
- pagesplit: true
- };
- html2pdf(html, pdf, function(pdf) {
- pdf.save('RequisationForm.pdf');
- });
- }
References for PDF files
These are the needed JS files to generate the SharePoint list data into a PDF file.
- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.min.js"></script>
- <script type="text/javascript" src="/teams/mahipal/SiteAssets/Billing Form/Scripts/Pdf.js"></script>
See the results on screen.
- Retrieve SharePoint list item and display in webpage, as below.
- Click on “Download PDF”.
A file has been downloaded.
- Let’s open the downloaded file.
Hence, we have downloaded the PDF successfully for the desired SharePoint list item.
Conclusion
In this article, we learned how to generate a PDF file from a SharePoint list item using AngularJS. This will help us to take out a particular SharePoint list item into a PDF file to make a hard copy.