Replies: 1 comment
-
async function renderPage(pageNumber, pageRenderElement) {
if (!pageRenderElement || !pageNumber || !doc) return;
const page = await doc.getPage(pageNumber);
const viewport = page.getViewport({ scale });
const outputScale = window.devicePixelRatio || 2;
pdfRenderContext = pageRenderElement.getContext("2d");
pageRenderElement.width = Math.floor(viewport.width * outputScale);
pageRenderElement.height = Math.floor(viewport.height * outputScale);
pageRenderElement.style.width = Math.floor(viewport.width) + "px";
pageRenderElement.style.height = Math.floor(viewport.height) + "px";
const transform =
outputScale !== 1 ? [outputScale, 0, 0, outputScale, 0, 0] : undefined;
await page.render({ canvasContext: pdfRenderContext, transform, viewport })
.promise;
textContainer.style.width = `${viewport.width}px`;
textContainer.style.height = `${viewport.height}px`;
// Clear previous text layer content
textContainer.innerHTML = '';
const textContent = await page.getTextContent();
const textLayer = new pdfjs.TextLayer({
container: textContainer,
textContentSource: textContent,
viewport,
enhanceTextSelection: true,
textLayerMode: 2,
});
textContainer.style.position = "absolute";
textContainer.style.left = "0";
textContainer.style.top = "0";
textContainer.style.width = `${viewport.width}px`;
textContainer.style.height = `${viewport.height}px`;
await textLayer.render();
// Scale the text layer container and its contents
textContainer.style.transform = `scale(${scale})`;
textContainer.style.transformOrigin = '0 0';
textContainer.style.width = `${viewport.width / scale}px`;
textContainer.style.height = `${viewport.height / scale}px`;
page.cleanup();
} Apparently this fixed the zoom problem for me |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
13unk0wn
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am using Svelte 5 and Tauri to build a PDF viewer. When I try to zoom in, the text does not render properly. Some text that renders correctly at scale
1.0
does not render at all at larger scales, and a lot of the text alignment is incorrect.If someone can help with this issue, I would be very grateful.
I am using:
pdfjs-dist@5.3.31
Svelte v5
Tauri v2
Here is the code
Beta Was this translation helpful? Give feedback.
All reactions