I am trying to generate pdf files in a cakePHP app, but so far get only html to be included in a file. The problem is that the main content of the page (calendar) is produced by the javascript which is completely ignored when generating a PDF. What is the best solution in this case?
I really appreciate your help.
If you use something like wkhtmltopdf it should work as it contains the actual rendering code used in chrome.
There is a plugin ready made that works out the box (after installing wkhtmltopdf)
Related
I am using wkhtmltopdf on Ubuntu 18.04 LTS from command line and am trying to create a pdf from a webpage. The webpage content is updated once a day and filled with current information by javascript.
When I use wkthmltopdf it renders all the "bare"/"empty" HTML but without current information included by the javascript, as if the javascript has not yet been loaded for the PDF or just ignored.
I am using wkhtmltopdf --javascript-delay 60000 --no-stop-slow-scripts --enable-javascript https://example.com/ test.pdf
Do you know how to create the pdf after the javascript has been loaded and filled the webpage with current information?
My solution:
After debugging the javascript via wkhtmltopdf (added option --debug-javascript) as #Nitin suggested I found that I had a SyntaxError (parse error).
I changed parts of the javascript from a "for..of" to "for..in" loop. This solved the problem an the pdf was created correctly. Thanks #Daphoque for explaining that this is probably due to the javascript version used by wkhtmltopdf.
I had an issue like this with jquery, but it was working fine with vanilla js.
Are you using javascript library ?
I have a requirement that I need to create a PDF. For me the best way is to do it render html template and create a PDF with any third party lib. I have come across the solution which render HTML with ejs and create pdf with html-pdf. It works fine though but I had a problem with a page break.
There is a popular module pdfkit. But it uses it own concept and procedure to render pdf. For node it does not render html file but for python it does render html template.
Please tell me how can i render html template to pdf using pdfKit and also what is the best way to render html and convert to pdf ?
Thanks
Puppeteer is the best way for converting HTML to the PDF, and also for web scraping.
A short instruction for generating PDF from HTML You can see here
Also, the Chrome DevTools team maintains the library, so it is the best solution.
About the page-break. This issue can be solved in the HTML code. There is a style option for solving the issue.
style="page-break-after:always;"
The problem with using PDF converter libraries available on NPM like pdfkit is that, you gonna have to recreate the page structures again in your html templates to get the desired output.
One of the best approach to rendering html and convert to pdf is by using Puppeteer on NodeJs. Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. It can be used to generate screenshots and PDFs of html pages in your case.
I recently ran into the problem related to html document validation. It seems to me that Chrome is pretty clever and can fix most of the mistakes.
In my case I receive raw html file with mistakes and I need to load it on Sumsung TV powered by Tizen 2.4. Unfortunately it doesn't provide the same features as Chrome so I need to fix documents by myself.
What do you think about html validation with help of javascript (My app is written in js).
Download html page and save it
1.2. Download all the related files (css,js,images) and fix links
fix all the problems (use some library, or may be there are some
good validators, but it is better to do offline)
Open document
You can use a linter; a quick search on Github showed up some JavaScript-powered HTML-linters:
htmllint: for HTML5
Bootling: for Bootstrap
I have SVG images, and lot of text with inline CSS, in an HTML page. I need to export all of them into PDF. Suggest to me proper technology which caters to my need. I am using JavaScript and jQuery as front end and Python as backend.
UPDATE:
I have tried jsPDF, but it is not preserving the CSS styling and I am not able to export images also.
Do you know the wkhtmltopdf command line tools? I was in a project where I also needed to convert a web page to PDF and I used this library (I used PHP as backend) and it worked very well for me.
I hope I have helped :D
If you want to do it server-side, there's a python package for that:
http://weasyprint.org/
Otherwise, you can do it in the browser but you will loose the CSS.
jsPDF appears to have experimental support for converting HTML to a PDF, or you can programmatically populate a PDF document with similar content. SVG is supported via plugin.
Demo:
http://rawgit.com/MrRio/jsPDF/master/
Github:
https://github.com/MrRio/jsPDF
In my html5 hybrid iPad application i am generating a report in one of the pages . What i want is to generate a PDF file of that report and save it in ipad.Please help me with this. I am using JavaScript and mobile jQuery for this.
It is highly inefficient to do this client-side(via Javascript). You should consider doing it server-side(using PHP, ASP/.NET or such). PHP uses a service called iTextSharp for this, PHP uses DOMPDF.
If you want to it with Javascript, http://html2canvas.hertzen.com/ is the best tool you will find! It uses the DOM style to generate an image. The only thing you need to do is then wrap it in a PDF. Have a look at their examples! For example:
http://html2canvas.hertzen.com/examples/artificial/index.html
You can use an open-source library for generating PDF documents, I have already use this library and works fine:
http://code.google.com/p/jspdf/
Try it, I hope that is what you want