I need to be able to click a button and save a modal window as a PDF. It needs to take the styling and all content inside the div the modal is built in.
I'm not tied to any specific library but I need to use a client-side library to to get this to work. My project is built with ASP.NET MVC & AngularJS.
I'm using pdfMake and html2canvas. Html2canvas is taking the div content and saving it as a canvas while pdfMake takes the canvas/image and saves it as a PDF.
This works great for my modal when there is a limited amount of data/records, usually around 15. Anytime more records are displayed, the PDF is rendered with just blank pages.
I'm not sure how to fix this so that the PDF's are rendered/generated with all of my data. I'm thinking it could be related to the HTML2Canvas portion and not necessarily with the data. The code examples from the developer are multiple pages of text and render fast and correctly. This tells me that I'm either missing some settings or there could be a comparability issue between HTML2Canvas and pdfMake.
I previously tried jsPDF and apparently it does not play well with CSS. I could not get the exported files to save with the correct styling.
I am using AngularJS ng-click to call the JS method printHistoricalData().
Here is my code that currently saves the HTML as a PDF when there is limited data records.
HTML
<button type="button" class="btn btn-default pull-right" ng-click="printHistoricalData()">PRINT</button>
<div id="historicalDataModal">
<div class="modal-body">
Modal Data Here
</div>
</div>
Angular/Javascript
function printHistoricalData() {
html2canvas(document.getElementById("historicalDataModal"), {
onrendered: function (canvas) {
var data = canvas.toDataURL("image/png");
var docDefinition = {
content: [
{
image: data,
width: 525,
alignment: "center"
}
],
pageSize: "letter",
pageOrientation: "portrait",
styles: {
footer: {
fontSize: 8,
alignment: "center",
margin: [0, 0, 0, 80]
}
}
};
pdfMake.createPdf(docDefinition).download($scope.selectedGroup.title + ".pdf");
}
});
}
Thank you for you help.