Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make wkhtmltopdf support ChartJS? #4767

Closed
xiranst opened this issue Sep 17, 2017 · 24 comments
Closed

How to make wkhtmltopdf support ChartJS? #4767

xiranst opened this issue Sep 17, 2017 · 24 comments

Comments

@xiranst
Copy link

xiranst commented Sep 17, 2017

wkhtmltopdf version: 12.4, 13 beta
ChartJs version is latest version: 2.7

in html page, the chart is working fine. but there is blank page after it was rendered with wkhtmltopdf.
how to solve it?

@simonbrunel
Copy link
Member

That's certainly because of these changes: #4591 and/or #4596

#4591 requires CSS animations to detect when the canvas is displayed, so you first need to figure out if wkhtmltopdf supports that feature (it might be better to report this issue on the wkhtmltopdf side).

@xiranst
Copy link
Author

xiranst commented Sep 19, 2017

@simonbrunel this chart is belonged to you? http://netdna.webdesignerdepot.com/uploads7/easily-create-stunning-animated-charts-with-chart-js/chartjs-demo.html
i can make pdf with this page. could you check it?

@etimberg
Copy link
Member

@xiranst that page uses Chart.js v1 which is quite old.

@xiranst
Copy link
Author

xiranst commented Sep 19, 2017

@etimberg yes, i know this chart.js is old. do you have the old document of this old version? because wkhtmltopdf support the old version not the latest version. i confuse why the old version worked in wkhtmltopdf not the latest version.

@etimberg
Copy link
Member

etimberg commented Oct 7, 2017

@xiranst the docs for the last release of v1 can be found in the docs folder of https://github.com/chartjs/Chart.js/releases/tag/v1.1.1

@kimyu92
Copy link

kimyu92 commented Nov 30, 2017

@xiranst I ran into the same issue, what you could do is, fall back to v2.6 for chart.js

wkhtmltopdf --javascript-delay 1000 test.html test.pdf

  <div style="width: 400px; height: 400px;">
    <canvas id="canvas" style="width: 400px; height: 400px;"></canvas>
  </div>

unless @etimberg @simonbrunel has better idea than this

caveat: it's blurry even with the settings below or approach to replace canvas with image #1961

  {..
     responsive: false,
     responsiveAnimationDuration: 0,
     animation: { duration: 0 },
  }

@aless673
Copy link

Same problem here.

Tried all versions of Chart.js and Wkhtmltopdf (Windows and CentOS)
Tried to fix size of canvas and its container.
Disabled animation.

But still not working.

Anyone has a solution ?

@zenichanin
Copy link

Anyone fix this problem yet? I've tried turning off animation and set specific dimensions on canvas and parent div element, yet still only blank image shows up in PDF.

@aless673
Copy link

I found a solution for my project, it may be useful for others like you @zenichanin :

If you need to navigate on a particular page to request the PDF you can draw the chart on that particular page with this rule :

visibility:hidden

Then in the chart options you can put this code to get an image of the chart :

var image = document.getElementById("chartId").toDataURL();

Then you can show this image (base 64 encoded) by using it directly in src attribute.
For example if you have a "Download PDF" button, just add the image as a POST parameter in the request

After all i want to advise you that the result quality isn't really satisfying (in my case). I switched to Google Chart because it uses SVG and it is perfect for rendering on a PDF !

@benmccann
Copy link
Contributor

benmccann commented Jun 17, 2018

It's not clear that there's any bug in Chart.js here. If there's some specific diagnosis of an issue and suggestion on a change we'd be happy to consider it

There appear to be multiple issues with wkhtmltopdf (wkhtmltopdf/wkhtmltopdf#4601 (comment)), so please track those issues in the wkhtmltopdf issue tracker

For questions, please ask on StackOverflow: https://stackoverflow.com/questions/tagged/chart.js

@admicaa
Copy link

admicaa commented Aug 6, 2018

You can see this working example right here https://github.com/admicaa/larabook

@syntaxc
Copy link

syntaxc commented Aug 9, 2018

I had a similar issue. (#5671)

Recommendation: Switch to C3 Charts. http://www.c3js.org

The charts look similar and are just as easy to configure.
They also worked perfectly with wkhtmltopdf for me.

@xiranst
Copy link
Author

xiranst commented Dec 18, 2018

@syntaxc thanks for your reply. i used c3 charts and the chart can show in pdf. but the fill-opacity of svg is not worked. did you have some suggestions?

@xiranst
Copy link
Author

xiranst commented Dec 18, 2018

1545104407744

@syntaxc
Copy link

syntaxc commented Dec 18, 2018

@xiranst What is the html/javascript code you used to create that chart?

@bhaveshevolve
Copy link

@simonbrunel this chart is belonged to you? http://netdna.webdesignerdepot.com/uploads7/easily-create-stunning-animated-charts-with-chart-js/chartjs-demo.html
i can make pdf with this page. could you check it?

Can you help me for more how you generating pdf.
Error display for me "The exit status code '-1073741819' says something went wrong:"

i am rendering charts using charts.js

@admicaa
Copy link

admicaa commented Feb 14, 2019

@bhaveshevolve, I made an article about making this stuff you can review it here
https://medium.com/@almestaadmicadiab/laravel-snappy-for-pdf-with-charts-e5c9b757d6dd

@itsmeJithin
Copy link

You can check out the working code here.

@radoy
Copy link

radoy commented Apr 9, 2020

@itsmeJithin thanks, the code running well..

@JamoCA
Copy link

JamoCA commented Nov 11, 2021

@itsmeJithin I think the polyfill makes the differences. I was able to use Chart.js 3.6.0 (with babel-polyfill 7) and it works! Thanks!

@alejandrombc
Copy link

@JamoCA can you provide an example of how to achieve it with chartjs 3.6.0?

@JamoCA
Copy link

JamoCA commented Nov 24, 2021

@alejandrombc Sorry. I thought it was v3.6, but it turns out that the sample from @itsmeJithin uses v2.8. (I wasn't paying attention.)

I've reposted the raw HTML for testing purposes. (The "itsme" sample required PHP which I don't use and isn't necessary in order to test this.)

I viewed the above referenced HTML using the QtWeb browser (v3.8.5 from 2013) and it doesn't render the Chart.js graphs either. (I'm aware that it uses an older version of the Qt engine, but it's what I've been successfullly using to aid in debugging WKHTMLTOPDF issues.)

After enabling QtWeb's ancient "Web Inspector", I see that there is a syntax/parse error on line 16 which makes me believe that the issue is due to ES6.

const requestAnimFrame = (function() {

I created an inline variable and confirmed that QtWeb threw the same syntax/parse error when encountering const.

<script>
'use strict';
const testVar = 1;

Upon further searching for solutions, I found this Chartjs github post that indicates that the issue is Qt's ES6 support.
#9381

One recommendation was to "compile chart.js yourself to ES5" using Babel.

I experimented with an chartjs-es5 library (which uses Chart.js 3.2.1). I additionally used a babel polyfill and proxy polyfill (based on console errors).

After doing this, I got a TypeError: 'undefined' is not an object (evaluating 'c.options.layout.padding') error. The ES5 library is minified, so it's difficult to troubleshoot any further.

I found a chartjs-plugin-background NPMJS library that seems to explain the process on how to compile Chart.js to ES5, but their demo uses v2.

Has anyone had any success with an ES5-compiled version of Chart.js v3?

@gbordoni
Copy link

gbordoni commented Aug 4, 2022

@JamoCA Do you hacve any news on this? I'm facing the same issue right not with ChartJs v3.8.

@JamoCA
Copy link

JamoCA commented Aug 4, 2022

I encountered another issue with WKHTMLTOPDF when it came to rendering content from Google Maps. (GM performs browser feature testing, determines that the Qt-enabled browser is outdated and displays a warning message instead of generating the map.)

As a result, I started reviewing alternatives that were affordable and could be hosted in our environment. The one that worked best for me (and was free/open-source) was Puppeteer which requires Node.js and uses Headless Chrome to render. I followed Puppeteer's screenshot demo and further modified the JS script to:

  1. accept a URL and OutputImageFilePath
  2. send a non-headless user-agent when performing the web request
  3. waiting until 'networkidle2' before capturing
  4. Identify the DIV (by ID) that I want to capture (which has static width/height defined in PX.)
  5. Save image (JPG or PNG) to the local file system

After I had a JPG/PNG screenshot of the rendered element, it was easy to include the static image in the WKHTMLTOPDF document that was being generated.

Here's some related links that I found to be beneficial:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests