mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-09 09:45:42 +02:00
Adds Streams API in getTextContent to stream data.
This patch adds Streams API support in getTextContent so that we can stream data in chunks instead of fetching whole data from worker thread to main thread. This patch supports Streams API without changing the core functionality of getTextContent. Enqueue textContent directly at getTextContent in partialEvaluator. Adds desiredSize and ready property in streamSink.
This commit is contained in:
parent
209751346c
commit
0c13d0ff46
8 changed files with 275 additions and 114 deletions
|
@ -950,6 +950,24 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
|
|||
return intentState.opListReadCapability.promise;
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {getTextContentParameters} params - getTextContent parameters.
|
||||
* @return {ReadableStream} ReadableStream to read textContent chunks.
|
||||
*/
|
||||
streamTextContent(params = {}) {
|
||||
const TEXT_CONTENT_CHUNK_SIZE = 100;
|
||||
return this.transport.messageHandler.sendWithStream('GetTextContent', {
|
||||
pageIndex: this.pageNumber - 1,
|
||||
normalizeWhitespace: (params.normalizeWhitespace === true),
|
||||
combineTextItems: (params.disableCombineTextItems !== true),
|
||||
}, {
|
||||
highWaterMark: TEXT_CONTENT_CHUNK_SIZE,
|
||||
size(textContent) {
|
||||
return textContent.items.length;
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {getTextContentParameters} params - getTextContent parameters.
|
||||
* @return {Promise} That is resolved a {@link TextContent}
|
||||
|
@ -957,10 +975,28 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
|
|||
*/
|
||||
getTextContent: function PDFPageProxy_getTextContent(params) {
|
||||
params = params || {};
|
||||
return this.transport.messageHandler.sendWithPromise('GetTextContent', {
|
||||
pageIndex: this.pageNumber - 1,
|
||||
normalizeWhitespace: (params.normalizeWhitespace === true),
|
||||
combineTextItems: (params.disableCombineTextItems !== true),
|
||||
let readableStream = this.streamTextContent(params);
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
function pump() {
|
||||
reader.read().then(function({ value, done, }) {
|
||||
if (done) {
|
||||
resolve(textContent);
|
||||
return;
|
||||
}
|
||||
Util.extendObj(textContent.styles, value.styles);
|
||||
Util.appendToArray(textContent.items, value.items);
|
||||
pump();
|
||||
}, reject);
|
||||
}
|
||||
|
||||
let reader = readableStream.getReader();
|
||||
let textContent = {
|
||||
items: [],
|
||||
styles: Object.create(null),
|
||||
};
|
||||
|
||||
pump();
|
||||
});
|
||||
},
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue