Skip to content

Commit

Permalink
WIP: Embed PDF with pdf.js
Browse files Browse the repository at this point in the history
  • Loading branch information
dasistwas committed Oct 12, 2023
1 parent 53d36d8 commit 9448729
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 42 deletions.
4 changes: 2 additions & 2 deletions amd/build/pdfembed.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion amd/build/pdfembed.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 26 additions & 10 deletions amd/src/pdfembed.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,38 @@
import * as pdfjsLib from 'mod_datalynx/pdf';
import pdfjsWorker from 'mod_datalynx/pdf.worker';

function renderPDFfunction(url, canvasContainer, options) {

var options = options || { scale: 1 };
function renderPDFfunction(url, canvasContainer) {

function renderPage(page) {
var viewport = page.getViewport(options.scale);
var viewport = page.getViewport({scale: 1});
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var renderContext = {
canvasContext: ctx,
viewport: viewport
};
// Calculate the scaling factors to fit the container's width and height
var widthScale = canvasContainer.clientWidth / viewport.width;
var heightScale = canvasContainer.clientHeight / viewport.height;

canvas.height = viewport.height;
canvas.width = viewport.width;
// Use the minimum scale to ensure that the entire page fits within the container
var scale = Math.min(widthScale, heightScale);

// Apply the scaling factor
canvas.width = viewport.width * scale;
canvas.height = viewport.height * scale;

// canvas.height = canvasContainer.clientHeight;
// canvas.width = canvasContainer.clientWidth;

// canvas.height = 800;
// canvas.width = 1200;

canvasContainer.appendChild(canvas);

console.log(canvasContainer, canvas);
// eslint-disable-next-line no-console
console.log(canvasContainer, canvas, canvas.height);

page.render(renderContext);
}
Expand All @@ -63,13 +76,16 @@ function renderPDFfunction(url, canvasContainer, options) {
}

// eslint-disable-next-line require-jsdoc
export function renderPDF() {
export function renderPDF(pdfUrl, canvasContainerId) {

// eslint-disable-next-line no-unused-vars
const pdf = M.cfg.wwwroot + '/mod/datalynx/tests/turnen.pdf';
const canvas = document.querySelector('#resourceobject').parentElement;
const container = document.querySelector(`#${canvasContainerId}`);

// eslint-disable-next-line no-console
console.log(pdfjsLib);
console.log(pdfUrl);
console.log(container);

renderPDFfunction(pdf, canvas);
};
renderPDFfunction(pdfUrl, container);
}
Loading

0 comments on commit 9448729

Please sign in to comment.