Skip to content

Commit

Permalink
perf: Remove custom handling for Tspan elements as react-pdf supports…
Browse files Browse the repository at this point in the history
… those natively now (reduces bundle size)
  • Loading branch information
EvHaus committed Nov 25, 2024
1 parent 30e560f commit 4574976
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
## The problem

One of the best ways to generate PDFs is using [`react-pdf`](https://react-pdf.org/). Unfortunately `react-pdf` has [several](https://github.com/diegomura/react-pdf/issues/1720) [major](https://github.com/diegomura/react-pdf/issues/1271) [annoying](https://github.com/diegomura/react-pdf/issues/2003) [bugs](https://github.com/diegomura/react-pdf/issues/2017) that makes rendering SVG charts very difficult. This library attempts to ease the pain and provide a possible workaround until those bugs are fixed.
One of the best ways to generate PDFs is using [`react-pdf`](https://react-pdf.org/). Unfortunately `react-pdf` has [several](https://github.com/diegomura/react-pdf/issues/1720) [major](https://github.com/diegomura/react-pdf/issues/1271) [bugs](https://github.com/diegomura/react-pdf/issues/2017) that makes rendering SVG charts very difficult. This library attempts to ease the pain and provide a possible workaround until those bugs are fixed.

## This solution

Expand Down
Binary file modified examples/recharts-basic.pdf
Binary file not shown.
Binary file modified examples/recharts-composed.pdf
Binary file not shown.
Binary file modified examples/victory-basic.pdf
Binary file not shown.
17 changes: 1 addition & 16 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,15 @@ const renderTextElement = ({
const { attribs } = node;
const { dx, dy } = getTspanChildrenOffsets(node as TagElementType);

let tSpanChildNode: JSX.Element | undefined;
const textChildren = React.Children.map(children, (child) => {
if (!child || typeof child === 'string') return child;

// TSpan elements are broken in react-pdf. This will
// convert them to plain text until the issue is fixed:
// https://github.com/diegomura/react-pdf/issues/2003
if (child.type === 'TSPAN') {
tSpanChildNode = child;
return child.props.children;
}

return child;
});

// If there's a TSpan child, we need to merge its styles with the node ones
const additionalStyle = tSpanChildNode
? (tSpanChildNode as JSX.Element).props.style
: null;

return (
<Text
{...baseProps}
style={getElementStyle(attribs, chartStyle, additionalStyle)}
style={getElementStyle(attribs, chartStyle)}
x={attribs.x != null ? Number.parseFloat(attribs.x) + dx : dx}
y={attribs.y != null ? Number.parseFloat(attribs.y) + dy : dy}
>
Expand Down

0 comments on commit 4574976

Please sign in to comment.