PageSourceSearch

https://thai-mecc.go.th/share/js/utils/PDFUtil.js

js thai-mecc.go.th collected 2026-10-09 03:35:25 UTC 1,574 bytes, 51 lines download raw bytes

1async function splitPDF(arrayBuffer) {
2	const { PDFDocument } = window.PDFLib;
3	let pages = [];
4
5	try {
6	  const pdfDoc = await PDFDocument.load(arrayBuffer);
7	  const numberOfPages = pdfDoc.getPageCount();
8
9	  for (let i = 0; i < numberOfPages; i++) {
10		const subDocument = await PDFDocument.create();
11		const [copiedPage] = await subDocument.copyPages(pdfDoc, [i]);
12		subDocument.addPage(copiedPage);
13
14		const pdfBytes = await subDocument.save();
15
16		const blob = new Blob([pdfBytes], { type: 'application/pdf' });
17		pages.push(blob);
18	  }
19
20	} catch (error) {
21		
22	}
23
24	return pages;
25}
26
27async function renderPDFToCanvasFromBlob(blob, pageNo, canvas = null) {
28    if (canvas == null) canvas = document.createElement('canvas');
29    pdfjsLib.GlobalWorkerOptions.workerSrc = `${rootURL}share/js/lib/pdf/pdf.worker.min.js`
30    let url = URL.createObjectURL(blob);
31    let loadingTask = pdfjsLib.getDocument(url);
32    let pdf = await loadingTask.promise;
33    renderPDFToCanvas(pdf, pageNo, canvas)
34    URL.revokeObjectURL(url);
35    return {canvas: canvas, pdf: pdf, count: pdf.numPages};
36}
37
38async function renderPDFToCanvas(pdf, pageNo, canvas = null) {
39    if (canvas == null) canvas = document.createElement('canvas');
40    let context = canvas.getContext('2d');
41    let page = await pdf.getPage(pageNo);
42    let viewport = page.getViewport({ scale: 1.0 });
43    canvas.height = viewport.height;
44    canvas.width = viewport.width;
45    let renderContext = {
46        canvasContext: context,
47        viewport: viewport
48    };
49    await page.render(renderContext);
50    return canvas;
51}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.