How to use the pdfjs-dist.getDocument function in pdfjs-dist

To help you get started, we’ve selected a few pdfjs-dist examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nusmodifications / nusmods / tasks / examTimetable.js View on Github external
function parseExamPdf(fileData) {
  const pdfjs = require('pdfjs-dist');
  const moment = require('moment');
  const _ = require('lodash');

  const fileArray = new Uint8Array(fileData);
  return pdfjs.getDocument(fileArray)
    .then((pdfDocument) => {
      // get all the pages from pdf
      const numPages = pdfDocument.numPages;
      return Promise.all(_.range(1, numPages + 1).map((pageNum) => {
        return pdfDocument.getPage(pageNum);
      }));
    })
    .then((pages) => {
      // get text content items from all pages
      return Promise.all(pages.map((page) => {
        return page.getTextContent().then((content) => {
          // remove headers and page number
          return content.items.slice(17, -1);
        });
      }));
    })
github rahul2104 / reactjs-pdf-reader / src / components / MobilePDFReader / index.tsx View on Github external
private open (params) {
    let url = params.url
    let self = this
    this.setTitleUsingUrl(url)
    // Loading document.
    let loadingTask = pdfjsLib.getDocument({
      url: url,
      withCredentials: true,
      maxImageSize: MAX_IMAGE_SIZE,
      cMapPacked: CMAP_PACKED
    })
    this.pdfLoadingTask = loadingTask

    loadingTask.onProgress = function (progressData) {
      self.progress(progressData.loaded / progressData.total)
    }

    return loadingTask.promise.then(function (pdfDocument) {
      // Document loaded, specifying document for the viewer.
      self.pdfDocument = pdfDocument;
      self.pdfViewer.setDocument(pdfDocument)
      self.pdfLinkService.setDocument(pdfDocument)
github oaeproject / Hilary / packages / oae-preview-processor / lib / processors / file / pdf.js View on Github external
const previewPDF = async function(ctx, pdfPath, callback) {
  domStubs.setStubs(global);

  const pagesDir = path.join(ctx.baseDir, PAGES_SUBDIRECTORY);
  const output = path.join(pagesDir, TXT_CONTENT_FILENAME);
  const data = new Uint8Array(fs.readFileSync(pdfPath));

  try {
    // Create a directory where we can store the files
    await fsMakeDir(pagesDir);

    // Will be using promises to load document, pages and misc data instead of
    // callback.
    const loadedPDFDocument = pdfjsLib.getDocument({
      data,
      // Try to export JPEG images directly if they don't need any further
      // processing.
      nativeImageDecoderSupport: pdfjsLib.NativeImageDecoding.NONE
    });

    const doc = await loadedPDFDocument.promise;
    const { numPages } = doc;

    ctx.addPreview(output, 'txt');
    ctx.addPreviewMetadata('pageCount', numPages);

    await processAllPages(ctx, pagesDir, numPages, doc);
    await fsWriteFile(output, pdfContents.join(' '));

    _generateThumbnail(ctx, pdfPath, pagesDir, callback);
github perchlabs / webird / app / webpack / modules / pdf / viewer / Page.vue View on Github external
async mounted() {
      const loadingTask = await getDocument(this.src).promise

      const page = await loadingTask.getPage(1)

      const {pdfCanvas: canvas, textLayer: textLayerDiv} = this.$refs

      const scale = this.dim[0] / page.getViewport({scale: 1.0}).width

      // Get viewport (dimensions)
      const viewport = page.getViewport({scale})
      const {width, height} = viewport

      // Fetch canvas' 2d context
      const context = canvas.getContext('2d')

      // Prepare object needed by render method
      const renderContext = {
github wojtekmaj / react-pdf / src / Document.jsx View on Github external
this.setState((prevState) => {
      if (!prevState.pdf) {
        return null;
      }

      return { pdf: null };
    });

    const { options, onLoadProgress, onPassword } = this.props;

    try {
      // If another loading is in progress, let's cancel it
      cancelRunningTask(this.runningTask);

      const loadingTask = pdfjs.getDocument({ ...source, ...options });
      loadingTask.onPassword = onPassword;
      if (onLoadProgress) {
        loadingTask.onProgress = onLoadProgress;
      }
      const cancellable = makeCancellable(loadingTask.promise);
      this.runningTask = cancellable;
      const pdf = await cancellable.promise;

      this.setState((prevState) => {
        if (prevState.pdf && prevState.pdf.fingerprint === pdf.fingerprint) {
          return null;
        }

        return { pdf };
      }, this.onLoadSuccess);
    } catch (error) {
github rahul2104 / reactjs-pdf-reader / src / components / PDFReader / index.tsx View on Github external
if (onDocumentComplete) {
                 this.props.onDocumentComplete(pdf.numPages);
              }
            }
          this.setState( { totalPage: pdf.numPages });
          this.setState({ pdf }, () => {
            if (showAllPage) {
              this.renderAllPage();
            } else {
              this.renderPage(dom, null);
            }
          });
        });
      } else {
        // loaded the base64
        const loadingTask = pdfjsLib.getDocument({data});
        loadingTask.promise.then((pdf) => {
          // is exit onDocumentComplete or not
            if(!showAllPage){
                if (onDocumentComplete) {
                    this.props.onDocumentComplete(pdf.numPages);
                }
            }
          this.setState({ pdf }, () => {
              if (showAllPage) {
                this.renderAllPage();
              } else {
                this.renderPage(dom, null);
              }
          });
        });
      }
github WorldBrain / Memex / src / activity-logger / background / pdffingerprint.ts View on Github external
export default async function getPdfFingerprint(pdfURL) {
    const PDFJS = require('pdfjs-dist')
    try {
        const pdf = await PDFJS.getDocument(pdfURL)
        return pdf.fingerprint
    } catch (err) {
        throw err
    }
}
github VadimDez / ng2-pdf-viewer / src / pdf-viewer / pdf-viewer.component.ts View on Github external
private fn() {
    PDFJS.getDocument(this._src).then((pdf: any) => {
      this._pdf = pdf;

      if (!this.isValidPageNumber(this._page)) {
        this._page = 1;
      }

      if (!this._showAll) {
        return this.renderPage(this._page);
      }

      return this.renderMultiplePages();
    });
  }
github arso-project / archipel / packages / plugin-pdf / frontend.js View on Github external
loadPDF () {
    this.loadingTask = PDFjs.getDocument({ data: this.props.content }).then((pdf) => {
      if (this.isUnmounting) return
      this.setState({ pdf })
    })
  }