excid3 / ferrum_pdf

A PDF generator for Rails using Ferrum & headless Chrome
MIT License
198 stars 6 forks source link

rails 8 importmap support (was: using chartkick and highcharts, my charts don't render) #22

Open andrewfader opened 6 days ago

andrewfader commented 6 days ago

implementing a pdf solution in a simple railsapp, and went with wkhtmltopdf/wicked_pdf originally, but I think ferrum_pdf should be able to render chartkick and highcharts since it's running the whole browser, but it still doesn't work. any suggestions on what I should debug or tweak? the charts are simply "Loading..."

andrewfader commented 6 days ago

Might this have something to do with the problem (on enabling js_errors)

/ruby/3.4.0-preview2/lib/ruby/gems/3.4.0+0/gems/puma-6.5.0/lib/puma/single.rb:63:in 'Thread#join': TypeError: Failed to resolve module specifier "application". Import Map: "application" matches with "application" but is blocked by a null value (Ferrum::JavaScriptError)
andrewfader commented 6 days ago

Ok, yeah, I've been doing some debugging and I think it's related to rails 8 style importmaps or propshaft, because if I add CDN links to the js files for chartkick and highcharts, it solves the problem.

andrewfader commented 6 days ago

I also need to add some sleeps in to get it to wait for the things to load - mght be a better way to do that?

ausangshukla commented 3 days ago

Did you figure this one out? The pdf is getting rendered with highcharts showing as loading... Need to wait for the charts and page to fully load, before rendering the pdf

andrewfader commented 3 days ago

@ausangshukla the fix for me was 2-fold 1) add the CDN links to my application layout

    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.4.1/chart.min.js"></script>
    <script src="https://code.highcharts.com/highcharts.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chartkick@5.0.1/dist/chartkick.min.js"></script>

2) forked ferrum_pdf to add sleeps https://github.com/andrewfader/ferrum_pdf/commit/975afc1d7fdd5aef521fabce16afcd516621da4a