[英]Is there a “Save Page As PDF” for Shiny app?
I posted an earlier question here about printing the page of a Shiny app to a pdf file for further use.我在这里发布了一个关于将 Shiny 应用程序的页面打印到 pdf 文件以供进一步使用的早期问题。
So I have found this Javascript code to print out certain CSS element to pdf所以我发现这个 Javascript 代码可以将某些 CSS 元素打印为 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).我把它放在我的 RShiny 项目文件夹中的 .js 文件中,并在我的 ui 函数中包含
includeScript("pdfOut.js")
(即 .js 文件的文件名)。 I also change the id of the element to those in my app as following:我还将元素的 id 更改为我的应用程序中的元素,如下所示:
$('#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
基本上,我想打印的元素
mainOrder_CA
(这是一个div)转换成PDF文件名test.pdf
,如果应用程序用户点击与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.
我对 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
基本上,我希望在用户单击
actionButton
时将div
元素打印为 pdf
Here is the original post for the JS code here这是JS代码的原始帖子here
You have to load the JS libraries jsPDF
and dom-to-image
.您必须加载 JS 库
jsPDF
和dom-to-image
。
library(shiny)
js <- "
$(document).ready(function(){
$('#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(); what is that?
});
});
});
"
ui <- fluidPage(
tags$head(
tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"),
tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/dom-to-image/2.6.0/dom-to-image.min.js"),
tags$script(js)
),
br(),
actionButton("printPdf_CA", "Print"),
div(
id = "mainOrder_CA",
h3("Click on the button to print me")
)
)
server <- function(input, output, session){
}
shinyApp(ui, server)
However, the pdf we get with this app is not nice, it does not respect the dimensions of the div
.然而,我们用这个应用程序得到的 pdf 并不好,它不尊重
div
的尺寸。 I don't know why.我不知道为什么。
Hmm, this works fine when one removes the margins of the h3
:嗯,当删除
h3
的边距时,这可以正常工作:
h3(style = "margin:0;", "Click on the button to print me")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.