div style width 100 height 20px button onclick showpage 1 style position absolute left 0px top 0px LEFT button button onclick showpage 1 style position absolute right 0px top 0px RIGHT button div script var script document createElement script script type text javascript script src mozilla github io pdf js build pdf js document head appendChild script let canvas document createElement canvas canvas id mycanvas canvas style width 320px canvas style height 470px canvas style border 1pt solid blue document body appendChild canvas If absolute URL from the remote server is provided configure the CORS header on that server var url https raw githubusercontent com mozilla pdf js ba2edeae web compressed tracemonkey pldi 09 pdf script onload function showpage var currentPage 1 async function showpage inc if typeof inc undefined inc 0 inc parseInt inc currentPage inc Loaded via script tag create shortcut to access PDF js exports var pdfjsLib window pdfjs dist build pdf The workerSrc property shall be specified pdfjsLib GlobalWorkerOptions workerSrc mozilla github io pdf js build pdf worker js let pdf await pdfjsLib getDocument url promise console log numpages pdf numPages if currentPage 1 currentPage 1 if currentPage pdf numPages currentPage pdf numPages console log showpage currentPage let page await pdf getPage currentPage let scale 1 5 let viewport page getViewport scale scale Prepare canvas using PDF page dimensions let canvas document getElementById mycanvas let context canvas getContext 2d canvas height viewport height canvas width viewport width Render PDF page into canvas context let renderContext canvasContext context viewport viewport wait for the rendering to complete var renderTask await page render renderContext console log ready script
cut to access PDF js exports var pdfjsLib window pdfjs dist build pdf The workerSrc property shall be specified pdfjsLib GlobalWorkerOptions workerSrc mozilla github io pdf js build pdf worker js let pdf await pdfjsLib getDocument url promise console log numpages pdf numPages if currentPage 1 currentPage 1 if currentPage pdf numPages currentPage pdf numPages console log showpage currentPage let page await pdf getPage currentPage let scale 1 5 let viewport page getViewport scale scale Prepare canvas using PDF page dimensions let canvas document getElementById mycanvas let context canvas getContext 2d canvas height viewport height canvas width viewport width Render PDF page into canvas context let renderContext canvasContext context viewport viewport wait for the rendering to complete var renderTask await page render renderContext console log ready script