Skip to content

Commit

Permalink
Fix an error when exporting a PDF with text annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
tnajdek committed Dec 17, 2024
1 parent 053f85c commit ded2a04
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/js/actions/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ const exportAttachmentWithAnnotations = itemKey => {
const attachmentURL = await dispatch(getAttachmentUrl(itemKey));
await dispatch(fetchAllChildItems(itemKey));
const state = getState();
const { pdfWorkerURL, pdfReaderCMapsRoot } = state.config;
const { pdfWorkerURL, pdfReaderCMapsURL, pdfReaderStandardFontsURL } = state.config;
const childItems = state.libraries[state.current.libraryKey]?.itemsByParent[itemKey]?.keys ?? [];
const allItems = state.libraries[state.current.libraryKey]?.items;
const attachmentItem = allItems[itemKey];
Expand All @@ -162,7 +162,7 @@ const exportAttachmentWithAnnotations = itemKey => {
.map(childItemKey => allItems[childItemKey])
.filter(item => !item.deleted && item.itemType === 'annotation');

const pdfWorker = new PDFWorker({ pdfWorkerURL, pdfReaderCMapsRoot });
const pdfWorker = new PDFWorker({ pdfWorkerURL, pdfReaderCMapsURL, pdfReaderStandardFontsURL });
const data = await (await fetch(attachmentURL)).arrayBuffer();
const buf = await pdfWorker.export(data, annotations);
const blob = new Blob([buf], { type: 'application/pdf' });
Expand Down
17 changes: 15 additions & 2 deletions src/js/common/pdf-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ export class PDFWorker {
let respData = null;
try {
if (message.action === 'FetchBuiltInCMap') {
let response = await fetch(this.config.pdfReaderCMapsRoot + message.data + '.bcmap');
let arrayBuffer = await response.arrayBuffer();
const response = await fetch(this.config.pdfReaderCMapsURL + message.data + '.bcmap');
const arrayBuffer = await response.arrayBuffer();
respData = {
compressionType: 1,
cMapData: new Uint8Array(arrayBuffer)
Expand All @@ -83,6 +83,19 @@ export class PDFWorker {
console.log('Failed to fetch CMap data:');
console.log(e);
}

try {
if (message.action === 'FetchStandardFontData') {
const response = await fetch(this.config.pdfReaderStandardFontsURL + message.data);
const arrayBuffer = await response.arrayBuffer();
respData = new Uint8Array(arrayBuffer);
}
}
catch (e) {
console.log('Failed to fetch standard font data:');
console.log(e);
}

this._worker.postMessage({ responseID: event.data.id, data: respData });
}
});
Expand Down
5 changes: 3 additions & 2 deletions src/js/component/reader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ const Reader = () => {
const currentUserID = useSelector(state => state.config.userId);
const currentUserSlug = useSelector(state => state.config.userSlug);
const pdfWorkerURL = useSelector(state => state.config.pdfWorkerURL);
const pdfReaderCMapsRoot = useSelector(state => state.config.pdfReaderCMapsRoot);
const pdfReaderCMapsURL = useSelector(state => state.config.pdfReaderCMapsURL);
const pdfReaderStandardFontsURL = useSelector(state => state.config.pdfReaderStandardFontsURL);
const tagColors = useSelector(state => state.libraries[libraryKey]?.tagColors?.value ?? []);
const { isGroupLibrary: isGroup, isReadOnly } = useSelector(state => state.config.libraries.find(l => l.key === libraryKey));
const pdfReaderURL = useSelector(state => state.config.pdfReaderURL);
Expand All @@ -185,7 +186,7 @@ const Reader = () => {
const isFetchingUserLibrarySettings = useSelector(state => state.libraries[userLibraryKey]?.settings?.isFetching);
const colorScheme = useSelector(state => state.preferences.colorScheme);
const useDarkModeForContent = useSelector(state => colorScheme !== 'light' && (state.preferences.useDarkModeForContent ?? true));
const pdfWorker = useMemo(() => new PDFWorker({ pdfWorkerURL, pdfReaderCMapsRoot }), [pdfReaderCMapsRoot, pdfWorkerURL]);
const pdfWorker = useMemo(() => new PDFWorker({ pdfWorkerURL, pdfReaderCMapsURL, pdfReaderStandardFontsURL }), [pdfReaderCMapsURL, pdfReaderStandardFontsURL, pdfWorkerURL]);

const [state, dispatchState] = useReducer(readerReducer, {
action: null,
Expand Down
3 changes: 2 additions & 1 deletion src/js/constants/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const stylesSourceUrl = 'https://www.zotero.org/styles-files/styles.json'
export const streamingApiUrl = 'wss://stream.zotero.org/';
export const translateUrl = 'location' in window ? window.location.origin : '';
export const pdfReaderURL = '/static/pdf-reader/reader.html';
export const pdfReaderCMapsRoot = '/static/pdf-reader/pdf/web/cmaps/';
export const pdfReaderCMapsURL = '/static/pdf-reader/pdf/web/cmaps/';
export const pdfReaderStandardFontsURL = '/static/pdf-reader/pdf/web/standard_fonts/';
export const pdfWorkerURL = '/static/pdf-worker/worker.js';
export const noteEditorURL = '/static/note-editor/editor.html';

Expand Down

0 comments on commit ded2a04

Please sign in to comment.