I posted an earlier question here about printing the page of a Shiny app to a pdf file for further use.
So I have found this Javascript code to print out certain CSS element to pdf
$('#downloadPDF').click(function () {
domtoimage.toPng(document.getElementById('content2'))
.then(function (blob) {
var pdf = new jsPDF('l', 'pt', [$('#content2').width(), $('#content2').height()]);
pdf.addImage(blob, 'PNG', 0, 0, $('#content2').width(), $('#content2').height());
pdf.save("test.pdf");
});
});
And I put that in a .js file in my RShiny project folder, and include includeScript("pdfOut.js")
in my ui function (that the filename of the .js file). I also change the id of the element to those in my app as following:
$('#printPdf_CA').click(function () {
domtoimage.toPng(document.getElementById('mainOrder_CA'))
.then(function (blob) {
var pdf = new jsPDF('l', 'pt', [$('#mainOrder_CA').width(), $('#mainOrder_CA').height()]);
pdf.addImage(blob, 'PNG', 0, 0, $('#mainOrder_CA').width(), $('#mainOrder_CA').height());
pdf.save("test.pdf");
that.options.api.optionsChanged();
});
});
Basically, I want to print the element mainOrder_CA
(which is a div) into a pdf file name test.pdf
if the app user clicks a button with id printPdf_CA
But it does not work. Nothing comes out, and there are no error message whatsoever. My app still runs as usual. I have zero knowledge on JS. Any advice on how to modify that code ? Basically, I am looking to print a div
element into pdf when a user clicks an actionButton
Here is the original post for the JS code here