Reputation: 107
I have stored some data "xyz" in java script local storage. Now I'm trying to access that in wicked pdf but unable to do so. It's working when i display as html but does not print that in the pdf.
That's what i have saved:
localStorage.setItem("my_id", "xyz");
In file.pdf.erb:
<script type="text/javascript">
setTimeout((function () {
//console.log(localStorage.getItem("dashboard_bar_chart_actual_situation_control"));
document.getElementById("p1").innerHTML = "HELLo "+localStorage.getItem("my_id");
window.status = "FLAG_FOR_PDF";
}), 1000);
</script>
Upvotes: 0
Views: 257
Reputation: 18784
Are you setting the localStorage value on the same page that you are rendering to PDF, or are you setting it on a prior page, and then expecting it to be retrieved on the PDF page?
The latter won't work in the default PDF rendering mode, because your HTML is saved to a file on-disk, and opened in a virtual browser, without any context (localStorage, cookies) of what happened on other pages (unless you supply them as options to wkhtmltopdf
, at least for cookies).
You could explicitly set it during render, so that it becomes available, something like this:
<script>
// Render it to the page, so it is executed when the JS runs:
localStorage.setItem("my_id", "<%= "xyz" %>")
// Use it:
document.getElementById("p1").innerHTML = "HELLo "+localStorage.getItem("my_id");
</script>
Alternatively, you can use the WickedPDF middleware, which attempts an in-place replacement of the HTML you see with a PDF version, without saving it to a temporary HTML file on-disk, but that also may not work well, depending on how the JS code gets executed.
Upvotes: 1