Skip to content

Replace wicked_pdf with ferrum_pdf for PDF generation#14221

Merged
mkllnk merged 5 commits into
openfoodfoundation:masterfrom
chahmedejaz:task/13547-replace-wicked_pdf-with-ferrum-pdf
May 21, 2026
Merged

Replace wicked_pdf with ferrum_pdf for PDF generation#14221
mkllnk merged 5 commits into
openfoodfoundation:masterfrom
chahmedejaz:task/13547-replace-wicked_pdf-with-ferrum-pdf

Conversation

@chahmedejaz

@chahmedejaz chahmedejaz commented Apr 27, 2026

Copy link
Copy Markdown
Collaborator

What? Why?

Replace wicked_pdf and wkhtmltopdf with ferrum_pdf for PDF generation across invoices and reporting.

wkhtmltopdf, the underlying tool currently used for PDF generation, has been archived and is no longer maintained. While wicked_pdf is still actively maintained, there are currently no plans to move away from supporting wkhtmltopdf, which makes our current PDF stack a growing piece of technical debt.

Key changes:

  • Remove wicked_pdf and wkhtmltopdf-binary dependencies.
  • Introduce a dedicated PdfRenderer service using FerrumPdf.render_pdf.
  • Replace invoice PDF rendering flow from render_with_wicked_pdf to send_data.
  • Remove wicked_pdf-specific helpers and initializers.
  • Replace wicked_pdf_image_tag with standard Rails image_tag.
  • Simplify stylesheet handling since Ferrum resolves relative asset paths using display_url.
  • Add Ferrum configuration for CI and Docker environments.
  • Adjust PDF scale (0.85) to better match previous wicked_pdf output and reduce layout regressions.
  • Add specs for the new rendering service and update invoice renderer specs.

This migration also removes several workarounds and monkey patches that previously existed to support wkhtmltopdf limitations.

What should we test?

  • Visit the admin order invoice page and print/download invoices.
  • Verify invoice PDFs render correctly for all types of invoice templates.
  • Verify distributor logos render correctly in generated PDFs.
  • Verify stylesheets and background colors are correctly applied in PDFs.
  • Verify long invoices still paginate correctly and table headers behave as expected across pages.
  • Verify reporting PDFs still generate correctly.
  • Compare generated PDFs against previous wicked_pdf output for layout consistency and scaling.
  • Verify other places from where we are generating PDFs if any

Release notes

Changelog Category (reviewers may add a label for the release notes):

  • User facing changes
  • API changes (V0, V1, DFC or Webhook)
  • Technical changes only
  • Feature toggled

Dependencies

@chahmedejaz chahmedejaz force-pushed the task/13547-replace-wicked_pdf-with-ferrum-pdf branch from 6a69b6e to 28a6cb6 Compare April 27, 2026 23:10
@chahmedejaz chahmedejaz added the pr-staged-au staging.openfoodnetwork.org.au label Apr 27, 2026
@dacook dacook removed the pr-staged-au staging.openfoodnetwork.org.au label Apr 28, 2026
@sigmundpetersen sigmundpetersen moved this from All the things 💤 to In Progress ⚙ in OFN Delivery board Apr 28, 2026
@chahmedejaz chahmedejaz force-pushed the task/13547-replace-wicked_pdf-with-ferrum-pdf branch from ce32f3e to f48d3df Compare May 9, 2026 19:39
@chahmedejaz chahmedejaz added tech debt technical changes only These pull requests do not contain user facing changes and are grouped in release notes labels May 9, 2026
@chahmedejaz chahmedejaz marked this pull request as ready for review May 9, 2026 20:24
@chahmedejaz chahmedejaz moved this from In Progress ⚙ to Code review 🔎 in OFN Delivery board May 9, 2026
@chahmedejaz chahmedejaz added pr-staged-fr staging.coopcircuits.fr pr-staged-au staging.openfoodnetwork.org.au and removed pr-staged-fr staging.coopcircuits.fr labels May 9, 2026
@chahmedejaz chahmedejaz moved this from Code review 🔎 to In Progress ⚙ in OFN Delivery board May 10, 2026
@chahmedejaz chahmedejaz marked this pull request as draft May 10, 2026 12:03
@chahmedejaz chahmedejaz removed the pr-staged-au staging.openfoodnetwork.org.au label May 10, 2026
@chahmedejaz chahmedejaz marked this pull request as ready for review May 10, 2026 18:42
@chahmedejaz chahmedejaz moved this from In Progress ⚙ to Code review 🔎 in OFN Delivery board May 15, 2026

@rioug rioug left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks mostly good, there are a couple of things to address.

Comment thread app/services/pdf_renderer.rb Outdated

class PdfRenderer
def render(html, display_url: default_display_url)
display_url ||= default_display_url

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems redundant, specifying the default in the method definition should be enough ? or maybe I am missing something ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it seems so; however, if by any chance display_url is passed as nil, then the default value would be overridden with nil, which will break the flow.

ref: https://github.com/openfoodfoundation/openfoodnetwork/pull/14221/changes#diff-6c6be13436a2ea06a1d07fb497d53f757f1113601f8f38f408134552bea87b4dR14-R42

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I think about it more, we could avoid sending display_url from here and just use the default value in the pdf renderer. I'll update that

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has been addressed here: 4b65460

config.process_timeout = 60
config.window_size = [1280, 800]

config.pdf_options.format = Rails.env.test? ? :A3 : :A4

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original comment says :

    # Conversion from PDF to text struggles with multi-line text.
    # We avoid that by printing on bigger pages.
    # https://github.com/openfoodfoundation/openfoodnetwork/pull/9674

Should we check if this still an issue ?
Comment from the issue : #9674 (review)
It's due to known bug, which have not been fixed. So I think we can keep it like this, but could you add the original comment ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Yes, I observed this as well while writing specs, and missed adding that comment. It's still an issue and a very annoying one 😅

It's addressed here: 4c27242

@github-project-automation github-project-automation Bot moved this from Code review 🔎 to In Progress ⚙ in OFN Delivery board May 15, 2026
@chahmedejaz chahmedejaz moved this from In Progress ⚙ to Code review 🔎 in OFN Delivery board May 15, 2026
@chahmedejaz

Copy link
Copy Markdown
Collaborator Author

Thanks for the feedback, @rioug. It's addressed and ready for review. Thanks

@rioug rioug left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much simpler, Thanks !

@mkllnk mkllnk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@mkllnk mkllnk moved this from Code review 🔎 to Test Ready 🧪 in OFN Delivery board May 18, 2026
@ValVerCH ValVerCH self-assigned this May 19, 2026
@ValVerCH ValVerCH added no-staging-AU A tag which does not trigger deployments, indicating a server is being used pr-staged-au staging.openfoodnetwork.org.au labels May 20, 2026
@ValVerCH

Copy link
Copy Markdown

Hi @openfoodfoundation/reviewers , I have tested it and all seems ok, see the PDF attached, only there is a point of Attention below that I detected on Staging_AU (not related to this issue I think, but strange maybe to fix to be sure our tests are correct with the logos of enterprises)

ATTENTION
There is no logo on the invoice at the top right, but instead there is the name of order cycle, here so called “Toute l’année”
And it appears the same after the PR deployment
So I guess it is something to be fixed on Staging_AU

OFN Testing - PDF generation#14221.pdf

@ValVerCH ValVerCH removed pr-staged-au staging.openfoodnetwork.org.au no-staging-AU A tag which does not trigger deployments, indicating a server is being used labels May 20, 2026
@RachL

RachL commented May 20, 2026

Copy link
Copy Markdown
Contributor

@ValVerCH FYI there are 2 invoices templates in OFN. Super admins can change the template here: https://staging.openfoodnetwork.org.au/admin/invoice_settings/edit

Only the alternative invoice template has the logo. So what you've seen is the expected behavior of the classic OFN template. All good :)

@RachL RachL moved this from Test Ready 🧪 to Ready to go 🚀 in OFN Delivery board May 20, 2026
@ValVerCH

ValVerCH commented May 20, 2026 via email

Copy link
Copy Markdown

@mkllnk

mkllnk commented May 21, 2026

Copy link
Copy Markdown
Member

I've installed Chromium on all production servers. Merging.

@mkllnk mkllnk merged commit 971d80f into openfoodfoundation:master May 21, 2026
64 of 65 checks passed
@github-project-automation github-project-automation Bot moved this from Ready to go 🚀 to Done in OFN Delivery board May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tech debt technical changes only These pull requests do not contain user facing changes and are grouped in release notes

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Replace wicked_pdf and wkhtmltopdf-binary gems

7 participants