Skip to content

Commit 295093f

Browse files
committed
fix: resolve images that contain special characters
1 parent 3f42a87 commit 295093f

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

.changeset/new-olives-wait.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"@react-pdf/image": patch
33
---
44

5-
Fix resolving images with space in filename
5+
Fix resolving images with special characters like space in filename

packages/image/src/resolve.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const getAbsoluteLocalPath = (src: string) => {
4545
path: pathname,
4646
} = url.parse(src);
4747

48-
const absolutePath = pathname ? path.resolve(pathname).replaceAll('%20', '') : undefined;
48+
const absolutePath = pathname ? path.resolve(src) : undefined;
4949

5050
if ((protocol && protocol !== 'file:') || auth || host || port || hostname) {
5151
return undefined;
118 KB
Loading
118 KB
Loading

packages/image/tests/resolve.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ describe('image resolveImage', () => {
9292
expect(image?.height).toBeGreaterThan(0);
9393
});
9494

95+
test('Should render a local image from absolute path', async () => {
96+
const absolutePath = path.join(__dirname, './assets/test.jpg');
97+
const image = await resolveImage({ uri: absolutePath });
98+
99+
expect(image?.data).toBeTruthy();
100+
expect(image?.width).toBeGreaterThan(0);
101+
expect(image?.height).toBeGreaterThan(0);
102+
});
103+
104+
105+
test('Should render a local image from relative path', async () => {
106+
const image = await resolveImage({
107+
uri: 'packages/layout/tests/assets/test.jpg',
108+
});
109+
110+
expect(image?.data).toBeTruthy();
111+
expect(image?.width).toBeGreaterThan(0);
112+
expect(image?.height).toBeGreaterThan(0);
113+
});
114+
115+
95116
test('Should render a local image from src object', async () => {
96117
const image = await resolveImage({
97118
uri: './packages/layout/tests/assets/test.jpg',
@@ -102,6 +123,26 @@ describe('image resolveImage', () => {
102123
expect(image?.height).toBeGreaterThan(0);
103124
});
104125

126+
test('Should render a local image with spaces in filename', async () => {
127+
const image = await resolveImage({
128+
uri: './packages/image/tests/assets/test with spaces.jpg',
129+
});
130+
131+
expect(image?.data).toBeTruthy();
132+
expect(image?.width).toBeGreaterThan(0);
133+
expect(image?.height).toBeGreaterThan(0);
134+
});
135+
136+
test('Should render a local image with special characters in filename', async () => {
137+
const image = await resolveImage({
138+
uri: './packages/image/tests/assets/special_ _%20_@&é"\'(§è!çà)-^$ù`,;:=?.+%£¨*<>.jpg',
139+
});
140+
141+
expect(image?.data).toBeTruthy();
142+
expect(image?.width).toBeGreaterThan(0);
143+
expect(image?.height).toBeGreaterThan(0);
144+
});
145+
105146
test('Should render a local image from data', async () => {
106147
const image = await resolveImage({ data: localJPGImage, format: 'jpg' });
107148

0 commit comments

Comments
 (0)