Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

fix(client): support text rendering for PDFs relying on system fonts (#996)

Proteus2a8bdec2ae3c
diff --git a/.changeset/ninety-lizards-visit.md b/.changeset/ninety-lizards-visit.md
new file mode 100644
index 00000000..b83a578e
--- /dev/null
+++ b/.changeset/ninety-lizards-visit.md
@@ -0,0 +1,5 @@
+---
+"@papra/app": patch
+---
+
+Fix preview of pdfs without embedded fonts by packaging cmaps and custom fonts in the app assets. Increasing the bundle size by ~2mb, but it's worth it for the improved UX and compatibility with a wider range of pdfs.
diff --git a/apps/papra-client/package.json b/apps/papra-client/package.json
index 3c7d1ce9..4cfd0e0b 100644
--- a/apps/papra-client/package.json
+++ b/apps/papra-client/package.json
@@ -68,6 +68,7 @@
"unocss": "catalog:",
"vite": "^7.1.9",
"vite-plugin-solid": "^2.11.9",
+ "vite-plugin-static-copy": "^4.1.0",
"vitest": "catalog:"
}
}
diff --git a/apps/papra-client/src/modules/documents/components/pdf-viewer/full-pdf-viewer.component.tsx b/apps/papra-client/src/modules/documents/components/pdf-viewer/full-pdf-viewer.component.tsx
index 0f459039..84fd68e5 100644
--- a/apps/papra-client/src/modules/documents/components/pdf-viewer/full-pdf-viewer.component.tsx
+++ b/apps/papra-client/src/modules/documents/components/pdf-viewer/full-pdf-viewer.component.tsx
@@ -1,10 +1,9 @@
import type { Component } from 'solid-js';
-import { usePDFSlick } from '@pdfslick/solid';
import { createSignal, onCleanup, onMount } from 'solid-js';
import { cn } from '@/modules/shared/style/cn';
import { SideBar } from './sidebar/sidebar.component';
import { PdfViewerToolbar } from './toolbar/pdf-viewer-toolbar.component';
-import '@pdfslick/solid/dist/pdf_viewer.css';
+import { usePdfViewer } from './use-pdf-viewer';
const SIDEBAR_DEFAULT_WIDTH = 233;
const SIDEBAR_MIN_WIDTH = 195;
@@ -16,7 +15,7 @@ export const PdfViewer: Component<{ url: string }> = (props) => {
thumbsRef,
pdfSlickStore: store,
PDFSlickViewer,
- } = usePDFSlick(props.url);
+ } = usePdfViewer({ url: props.url });
const [isSidebarOpen, setIsSidebarOpen] = createSignal(true);
const [sidebarWidth, setSidebarWidth] = createSignal(SIDEBAR_DEFAULT_WIDTH);
diff --git a/apps/papra-client/src/modules/documents/components/pdf-viewer/simple-pdf-viewer.component.tsx b/apps/papra-client/src/modules/documents/components/pdf-viewer/simple-pdf-viewer.component.tsx
index 5188018d..2c123ad7 100644
--- a/apps/papra-client/src/modules/documents/components/pdf-viewer/simple-pdf-viewer.component.tsx
+++ b/apps/papra-client/src/modules/documents/components/pdf-viewer/simple-pdf-viewer.component.tsx
@@ -1,14 +1,12 @@
import type { Component } from 'solid-js';
-import { usePDFSlick } from '@pdfslick/solid';
-
-import '@pdfslick/solid/dist/pdf_viewer.css';
+import { usePdfViewer } from './use-pdf-viewer';
export const SimplePdfViewer: Component<{ url: string }> = (props) => {
const {
viewerRef,
pdfSlickStore: store,
PDFSlickViewer,
- } = usePDFSlick(props.url);
+ } = usePdfViewer({ url: props.url });
return (
<div class="pdfSlick relative w-full h-full">
diff --git a/apps/papra-client/src/modules/documents/components/pdf-viewer/use-pdf-viewer.ts b/apps/papra-client/src/modules/documents/components/pdf-viewer/use-pdf-viewer.ts
new file mode 100644
index 00000000..ad1f4255
--- /dev/null
+++ b/apps/papra-client/src/modules/documents/components/pdf-viewer/use-pdf-viewer.ts
@@ -0,0 +1,15 @@
+import { usePDFSlick } from '@pdfslick/solid';
+import '@pdfslick/solid/dist/pdf_viewer.css';
+
+export function usePdfViewer({ url }: { url: string }) {
+ return usePDFSlick(url, {
+ // These assets are required to render PDFs with non-embedded standard system fonts
+ // those assets are automatically copied from the pdfjs-dist package
+ // see https://github.com/papra-hq/papra/pull/996
+ getDocumentParams: {
+ cMapUrl: `/pdfjs-assets/cmaps/`,
+ cMapPacked: true,
+ standardFontDataUrl: `/pdfjs-assets/standard_fonts/`,
+ },
+ });
+}
diff --git a/apps/papra-client/vite.config.ts b/apps/papra-client/vite.config.ts
index 4d7f7606..77eda36a 100644
--- a/apps/papra-client/vite.config.ts
+++ b/apps/papra-client/vite.config.ts
@@ -1,18 +1,36 @@
import type { Plugin } from 'vite';
import fs from 'node:fs';
+import { createRequire } from 'node:module';
import path from 'node:path';
import { env } from 'node:process';
import unoCssPlugin from 'unocss/vite';
import { defineConfig } from 'vite';
import solidPlugin from 'vite-plugin-solid';
+import { viteStaticCopy } from 'vite-plugin-static-copy';
const isDemoMode = env.VITE_IS_DEMO_MODE === 'true';
+const pdfjsAssetsDirectory = getPdfjsAssetsDirectoryPath();
+
export default defineConfig({
plugins: [
unoCssPlugin(),
solidPlugin(),
cleanDemoAssetsPlugin(),
+ viteStaticCopy({
+ targets: [
+ {
+ src: path.join(pdfjsAssetsDirectory, 'cmaps'),
+ dest: 'pdfjs-assets/cmaps',
+ rename: { stripBase: true },
+ },
+ {
+ src: path.join(pdfjsAssetsDirectory, 'standard_fonts'),
+ dest: 'pdfjs-assets/standard_fonts',
+ rename: { stripBase: true },
+ },
+ ],
+ }),
],
server: {
port: 3000,
@@ -38,6 +56,16 @@ export default defineConfig({
// },
});
+function getPdfjsAssetsDirectoryPath(): string {
+ const require = createRequire(import.meta.url);
+ const pdfjsDistDir = path.dirname(
+ require.resolve('pdfjs-dist/package.json', {
+ paths: [require.resolve('@pdfslick/core/package.json')],
+ }),
+ );
+ return pdfjsDistDir;
+}
+
function cleanDemoAssetsPlugin(): Plugin {
return {
name: 'clean-demo-assets',
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 1a90528b..4ffc4710 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -431,6 +431,9 @@ importers:
vite-plugin-solid:
specifier: ^2.11.9
+ vite-plugin-static-copy:
+ specifier: ^4.1.0
vitest:
specifier: 'catalog:'
@@ -6006,6 +6009,10 @@ packages:
resolution: {integrity: sha512-Ko0uX15oIUS7wJ3Rb30Fs6SkVbLmPBAKdlm7q9+ak9bbIeFf0MwuBsQV6z7+X768/cHsfg+WlysDWJcmthjsjQ==}
+ resolution: {integrity: sha512-Ceh+7ox5qe7LJuLHoY0feh3pHuUDHAcRUeyL2VYghZwfpkNIy/+8Ocg0a3UuSoYzavmylwuLWQOf3hl0jjMMIw==}
+ engines: {node: '>=8'}
+
resolution: {integrity: sha512-VSWO/W6nNQdyP520F1mhf+Lc2f8pjGQOtoHHm7Ze8Go1kX7akpVIrtTa0fn+HB0QJEDVacl6aO08YE0PgXfdnQ==}
@@ -6185,6 +6192,10 @@ packages:
resolution: {integrity: sha512-yrLQ/yVUFXkzg7EDQsPieE/53+0RlaWTs+wBrvW36cyilJ2SaDWfl4Yj7MtLTXleV9uEKefbAGUPv2/iWSooRA==}
+ resolution: {integrity: sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==}
+ engines: {node: '>= 8.10.0'}
+
resolution: {integrity: sha512-Qgzu8kfBvo+cA4962jnP1KkS6Dop5NS6g7R5LFYJr4b8Ub94PPQXUksCw9PvXoeXPRRddRNC5C1JQUR2SMGtnA==}
engines: {node: '>= 14.16.0'}
@@ -8203,6 +8214,10 @@ packages:
resolution: {integrity: sha512-n4ZT37wG78iz03xPRKJrHTdZbe3IicyucEtdRsV5yglwc3GyUfbAfpSeD0FJ41NbUNSt5wbhqfp1fS+BgnvDFQ==}
engines: {node: '>= 0.4'}
+ resolution: {integrity: sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==}
+ engines: {node: '>=8'}
+
resolution: {integrity: sha512-wa56o2/ElJMYqjCjGkXri7it5FbebW5usLw/nPmCMs5DeZ7eziSYZhSmPRn0txqeW4LnAmQQU7FgqLpsEFKM4A==}
engines: {node: '>= 0.4'}
@@ -9574,6 +9589,10 @@ packages:
resolution: {integrity: sha512-y3b8Kpd8OAN444hxfBbFfj1FY/RjtTd8tzYwhUqNYXx0fXx2iX4maP4Qr6qhIKbQXI02wTLAda4fYUbDagTUFw==}
engines: {node: '>=6'}
+ resolution: {integrity: sha512-tkAQEw8ysMzmkhgw8k+1U/iPhWNhykKnSk4Rd5zLoPJCuJaGRPo6YposrZgaxHKzDHdDWWZvE/Sk7hsL2X/CpQ==}
+ engines: {node: '>=18'}
+
resolution: {integrity: sha512-aNZ+VfjobsWryoiPnEApGGmf5WmNsCo9xu8dfaYamG5qaLP7ClhLN6NgsFe6SwJ2UbLEBK5dv9x8Mn5+RVhMWQ==}
engines: {node: '>=18'}
@@ -10085,6 +10104,10 @@ packages:
resolution: {integrity: sha512-v05I2k7xN8zXvPD9N+z/uhXPaj0sUFCe2rcWZIpBsqxfP7xXFQ0tipAd/wjj1YxWyWtUS5IDJpOG82JKt2EAVA==}
+ resolution: {integrity: sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==}
+ engines: {node: '>=8.10.0'}
+
resolution: {integrity: sha512-GDhwkLfywWL2s6vEjyhri+eXmfH6j1L7JE27WhqLeYzoh/A3DBaYGEj2H/HFZCn/kMfim73FXxEJTw06WtxQwg==}
engines: {node: '>= 14.18.0'}
@@ -11495,6 +11518,12 @@ packages:
'@testing-library/jest-dom':
optional: true
+ resolution: {integrity: sha512-9XOarNV7LgP0KBB7AApxdgFikLXx3daZdqjC3AevYsL6MrUH62zphonLUs2a6LZc1HN1GY+vQdheZ8VVJb6dQQ==}
+ engines: {node: ^22.0.0 || >=24.0.0}
+ peerDependencies:
+ vite: ^6.0.0 || ^7.0.0 || ^8.0.0
+
resolution: {integrity: sha512-+Oxm7q9hDoLMyJOYfUYBuHQo+dkAloi33apOPP56pzj+vsdJDzr+j1NISE5pyaAuKL4A3UD34qd0lx5+kfKp2g==}
engines: {node: ^18.0.0 || ^20.0.0 || >=22.0.0}
@@ -18340,6 +18369,8 @@ snapshots:
+
@@ -18522,6 +18553,18 @@ snapshots:
+ dependencies:
+ anymatch: 3.1.3
+ braces: 3.0.3
+ glob-parent: 5.1.2
+ is-binary-path: 2.1.0
+ is-glob: 4.0.3
+ normalize-path: 3.0.0
+ readdirp: 3.6.0
+ optionalDependencies:
+ fsevents: 2.3.3
+
dependencies:
readdirp: 4.1.2
@@ -21005,6 +21048,10 @@ snapshots:
dependencies:
has-bigints: 1.1.0
+ dependencies:
+ binary-extensions: 2.3.0
+
dependencies:
call-bound: 1.0.4
@@ -22884,6 +22931,8 @@ snapshots:
+
dependencies:
eventemitter3: 5.0.1
@@ -23455,6 +23504,10 @@ snapshots:
dependencies:
minimatch: 5.1.6
+ dependencies:
+ picomatch: 2.3.1
+
@@ -25167,6 +25220,14 @@ snapshots:
transitivePeerDependencies:
- supports-color
+ dependencies:
+ chokidar: 3.6.0
+ p-map: 7.0.4
+ picocolors: 1.1.1
+ tinyglobby: 0.2.15
+
dependencies:
esbuild: 0.25.10