Best JavaScript code snippet using wpt
pdfjs-text-layer.component.ts
Source:pdfjs-text-layer.component.ts
1import {Component, ElementRef, Inject, Input, OnDestroy, OnInit} from '@angular/core';2import {PDFPageProxy, PDFPageViewport, TextContent} from 'pdfjs-dist';3import {DOCUMENT} from '@angular/common';4import {PdfApi, TextLayerRenderTask} from '../../classes/pdfapi';5import {PDF_API} from '../../classes/pdfjs-objects';6@Component({7 selector: 'pdfjs-text-layer',8 template: `<div class="endOfContent"></div>`,9 styleUrls: ['./pdfjs-text-layer.component.scss']10})11export class PdfjsTextLayerComponent implements OnInit, OnDestroy {12 private innerViewport: PDFPageViewport;13 private innerPdfPageProxy: PDFPageProxy;14 private textLayerRenderTask: TextLayerRenderTask;15 constructor(@Inject(DOCUMENT) private document: Document,16 @Inject(PDF_API) private API: PdfApi,17 private elementRef: ElementRef) {18 }19 @Input()20 get viewport(): PDFPageViewport {21 return this.innerViewport;22 }23 set viewport(viewport: PDFPageViewport) {24 if (this.innerViewport !== viewport) {25 this.innerViewport = viewport;26 this.writeTextLayer();27 }28 }29 @Input()30 get pdfPageProxy(): PDFPageProxy {31 return this.innerPdfPageProxy;32 }33 set pdfPageProxy(pdfPageProxy: PDFPageProxy) {34 if (this.innerPdfPageProxy !== pdfPageProxy) {35 this.innerPdfPageProxy = pdfPageProxy;36 this.writeTextLayer();37 }38 }39 ngOnInit() {40 }41 ngOnDestroy() {42 this.cancelTextLayerRenderTask();43 }44 private cancelTextLayerRenderTask() {45 if (!!this.textLayerRenderTask && this.textLayerRenderTask.cancel) {46 this.textLayerRenderTask.cancel();47 }48 }49 private writeTextLayer() {50 this.cancelTextLayerRenderTask();51 if (!!this.viewport && !!this.pdfPageProxy) {52 const container = this.document.createDocumentFragment();53 this.pdfPageProxy.getTextContent().then((textContent: TextContent) => {54 this.textLayerRenderTask = this.API.renderTextLayer({55 textContent,56 container,57 viewport: this.viewport58 });59 this.textLayerRenderTask.promise.then(() => {60 this.clear();61 (this.elementRef.nativeElement as HTMLElement).insertBefore(container, this.elementRef.nativeElement.firstChild);62 }, (error: any) => {63 console.log('error', error);64 if (error.name !== 'RenderingCancelledException') {65 console.log('render textLayer error', error);66 }67 });68 });69 } else {70 this.clear();71 }72 }73 /**74 * Reset text layout75 */76 private clear() {77 this.elementRef.nativeElement.innerHTML = '<div class="endOfContent"></div>';78 }...
CachedPDFjsWrapper.js
Source:CachedPDFjsWrapper.js
1const pdfjs = require('pdfjs-dist/webpack');2export class CachedPDFjsWrapper {3 static cache = {};4 static renderPage(renderContext, filepath, pdfPageProxy) {5 let renderTask = pdfPageProxy.render(renderContext);6 renderTask.promise.then(() => {7 this.handleRenderCompleted(filepath, pdfPageProxy, renderTask);8 });9 }10 static unsubscribe(filepath) {11 if (filepath in this.cache) {12 this.cache[filepath]['referenceCount']--;13 if (this.cache[filepath]['referenceCount'] <= 0) {14 let pdfDocumentProxyPromise = this.cache[filepath]['PDFDocumentProxyPromise'];15 delete this.cache[filepath];16 pdfDocumentProxyPromise.then(pdfDocumentProxy => {17 pdfDocumentProxy.cleanup();18 pdfDocumentProxy.destroy();19 });20 }21 }22 }23 // åç
§ã«ã¦ã³ãå½¢å¼ã®GCã¿ãããªãã®24 static handleRenderCompleted(filepath, pdfPageProxy, renderTask) {25 renderTask.cancel();26 pdfPageProxy.cleanup();27 this.unsubscribe(filepath);28 }29 static getPDFDocumentProxyPromise(filepath) {30 if (filepath in this.cache) {31 this.cache[filepath]['referenceCount']++;32 return this.cache[filepath]['PDFDocumentProxyPromise'];33 }34 let origin = new URL(window.location.href).origin;35 let url = origin + '/' + filepath;36 let loadingTask = pdfjs.getDocument(url);37 this.cache[filepath] = {};38 this.cache[filepath]['referenceCount'] = 1;39 this.cache[filepath]['PDFDocumentProxyPromise'] = new Promise(40 (resolve, reject) => {41 loadingTask.promise.then(pdfDocumentProxy => {42 resolve(pdfDocumentProxy);43 }).catch(e => reject(e));44 }45 );46 return this.cache[filepath]['PDFDocumentProxyPromise'];47 }48 static getPDFPageProxyPromise(filepath, pageNumber) {49 return new Promise((resolve, reject) => {50 this.getPDFDocumentProxyPromise(filepath).then(pdfDocumentProxy => {51 pdfDocumentProxy.getPage(pageNumber).then(pdfPageProxy => {52 resolve(pdfPageProxy);53 }).catch(e => reject(e));54 });55 });56 }...
pdf-page.ts
Source:pdf-page.ts
1import { PDFPageProxy } from 'pdfjs-dist';2import { PdfOrientation } from './pdf-orientation';3const VIEW_MAX_X_INDEX = 2;4const VIEW_MAX_Y_INDEX = 3;5// wrapper for PDFPageProxy class from pdf.js6export class PdfPage {7 private readonly pageProxy: PDFPageProxy;8 public orientation: PdfOrientation;9 constructor(pageProxy: PDFPageProxy) {10 this.pageProxy = pageProxy;11 if (this.height > this.width) {12 this.orientation = 'vertical';13 } else {14 this.orientation = 'horizontal';15 }16 }17 public get width(): number {18 return this.pageProxy.view[VIEW_MAX_X_INDEX];19 }20 public get height(): number {21 return this.pageProxy.view[VIEW_MAX_Y_INDEX];22 }23 public render(24 canvasContext: CanvasRenderingContext2D,25 ): Promise<PDFPageProxy> {26 return this.pageProxy.render({27 canvasContext,28 viewport: this.pageProxy.getViewport({ scale: 1 }),29 }).promise;30 }31 public renderScaled(canvas: HTMLCanvasElement): Promise<PDFPageProxy> {32 const currentViewport = this.pageProxy.getViewport({ scale: 1 });33 const scale = canvas.width / currentViewport.width;34 const viewport = this.pageProxy.getViewport({ scale });35 canvas.height = viewport.height;36 const canvasContext = canvas.getContext('2d');37 if (!canvasContext) {38 throw new Error('Cannot render page. Canvas context is null.');39 }40 return this.pageProxy.render({41 canvasContext: canvasContext,42 viewport,43 }).promise;44 }...
Using AI Code Generation
1var viewport = page.getViewport(scale);2var textLayerDiv = document.createElement('div');3textLayerDiv.className = 'textLayer';4textLayerDiv.style.width = viewport.width + 'px';5textLayerDiv.style.height = viewport.height + 'px';6page.getTextContent().then(function (textContent) {7 var textLayer = new TextLayerBuilder({8 });9 textLayer.setTextContent(textContent);10 textLayer.render();11});12function TextLayerBuilder(options) {13 this.textLayerDiv = options.textLayerDiv;14 this.layoutDone = false;15 this.divContentDone = false;16 this.pageIdx = options.pageIndex;17 this.matches = [];18 this.viewport = options.viewport;19 this.textDivs = [];20}21TextLayerBuilder.prototype = {22 setTextContent: function TextLayerBuilder_setTextContent(textContent) {23 this.textContent = textContent;24 this.render(TEXT_LAYER_RENDER_DELAY);25 },26 render: function TextLayerBuilder_render(timeout) {27 var self = this;28 if (!timeout) {29 timeout = 0;30 }31 if (!this.textContent) {32 return;33 }34 this.cancel();35 this.textDivs = [];36 this.renderTimer = setTimeout(function () {37 self.renderTimeout();38 }, timeout);39 },40 renderTimeout: function TextLayerBuilder_renderTimeout() {41 var textLayerFrag = document.createDocumentFragment();42 this.textContent.items.forEach(function (textItem) {43 var textDiv = document.createElement('div');44 textDiv.setAttribute('style', 'font-size: 16px; position: absolute;');45 this.textDivs.push(textDiv);46 if (textItem.str === ' ') {47 textDiv.dataset.isWhitespace = true;48 }49 appendText(textDiv, textItem.str);50 textLayerFrag.appendChild(textDiv);51 }, this);52 this.textLayerDiv.appendChild(textLayerFrag);53 this.renderTimer = null;54 },55 cancel: function TextLayerBuilder_cancel() {56 clearTimeout(this.renderTimer);57 this.renderTimer = null;58 },59 setTextContentStream: function TextLayerBuilder_setTextContentStream(readableStream) {
Using AI Code Generation
1var pdf = PDFJS.getDocument('test.pdf');2pdf.then(function(pdf) {3 pdf.getPage(1).then(function(page) {4 page.getTextContent().then(function(textContent) {5 var pageProxy = new PDFPageProxy(pdf, page.pageIndex, textContent);6 pageProxy.getTextContent().then(function(textContent) {7 console.log(textContent);8 });9 });10 });11});
Using AI Code Generation
1function renderTextLayer(page){2 var textLayerDiv = document.createElement("div");3 textLayerDiv.className = "textLayer";4 var canvas = document.getElementById("page" + (page.pageIndex + 1));5 var viewport = page.getViewport(1.0);6 textLayerDiv.style.width = viewport.width + "px";7 textLayerDiv.style.height = viewport.height + "px";8 canvas.parentNode.insertBefore(textLayerDiv, canvas);9 var textLayer = new TextLayerBuilder({10 });11 textLayer.setTextContent(page.getTextContent());12 textLayer.render();13}14function renderPageLayer(page){15 var canvas = document.getElementById("page" + (page.pageIndex + 1));16 var viewport = page.getViewport(1.0);17 var pageLayer = new PageLayerBuilder({18 });19 pageLayer.render();20}21function renderPageLayer(page){22 var canvas = document.getElementById("page" + (page.pageIndex + 1));23 var viewport = page.getViewport(1.0);24 var pageLayer = new PageLayerBuilder({25 });26 pageLayer.render();27}28function renderPageLayer(page){29 var canvas = document.getElementById("page" + (page.pageIndex + 1));30 var viewport = page.getViewport(1.0);31 var pageLayer = new PageLayerBuilder({32 });33 pageLayer.render();34}35function renderPageLayer(page){36 var canvas = document.getElementById("page" + (page.pageIndex + 1));37 var viewport = page.getViewport(1.0);38 var pageLayer = new PageLayerBuilder({39 });
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!