Skip to content

Conversation

@eKoopmans
Copy link
Owner

This PR should improve the cloning done by html2pdf.js as part of the "toContainer" stage, by using the core deepClone logic from @zumer/snapdom with a few changes:

  • Removed inlineAllStyles logic, because this was hard-coding things like width - I specifically want things to reflow with the chosen PDF document size
    • That said, some styles could definitely get lost by not inlining them this way
    • A future alternative could be to inline all styles except width/height and top/left/right/bottom etc., but that gets tricky verifying the full list of styles to exclude and possible side-effects
  • Also removed "exclude"/"placeholder" logic, this is snapdom-specific and would be taken care of by it directly if I use it for image generation
  • Removed everything related to styleMap, styleCache, nodeMap, etc.
  • Added the scrolling fix from prepareClone
    • It's possible there are some other prepareClone pieces I should be keeping, but scrolling was in my existing clone logic and I didn't want to lose it

Big picture, I was hoping to eliminate my own cloning step entirely and rely only on snapdom's cloning as part of its image-creation process - unfortunately that won't be possible. html2pdf.js relies on reflowing the content to capture it in a specified PDF page size, and I don't want to resize the source element - that means there needs to be some clone that happens before styles are inlined. In snapdom, those inline styles are always calculated from the source elements, so there pretty much needs to be a preliminary clone step before it can do its work.

In local testing this new clone process still needs some time after the clone to allow things to properly reflow, so I'm leaving in the 10ms delay after cloning.

@eKoopmans eKoopmans merged commit 54c50fb into main Aug 31, 2025
2 of 5 checks passed
@eKoopmans eKoopmans deleted the ekoopmans/improve-clone branch August 31, 2025 04:21
@ekoopmans-github-release-tokens
Copy link
Contributor

🎉 This PR is included in version 0.12.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants