{"id":13433029,"url":"https://github.com/nisaacson/pdf-extract","last_synced_at":"2025-05-16T12:02:23.440Z","repository":{"id":5214528,"uuid":"6390454","full_name":"nisaacson/pdf-extract","owner":"nisaacson","description":"Node PDF Extract","archived":false,"fork":false,"pushed_at":"2023-08-14T19:28:55.000Z","size":3548,"stargazers_count":389,"open_issues_count":23,"forks_count":76,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-05-11T23:46:06.446Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"rails/rails","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nisaacson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-10-25T16:28:09.000Z","updated_at":"2025-03-20T08:19:15.000Z","dependencies_parsed_at":"2024-06-18T15:22:16.590Z","dependency_job_id":"379e97ca-46dd-4422-9298-17f6ae0350c5","html_url":"https://github.com/nisaacson/pdf-extract","commit_stats":{"total_commits":94,"total_committers":15,"mean_commits":6.266666666666667,"dds":0.3191489361702128,"last_synced_commit":"d334b3a497de830ca825f1fdd94e9588e890563f"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nisaacson%2Fpdf-extract","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nisaacson%2Fpdf-extract/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nisaacson%2Fpdf-extract/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nisaacson%2Fpdf-extract/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nisaacson","download_url":"https://codeload.github.com/nisaacson/pdf-extract/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254527074,"owners_count":22085918,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T02:01:20.024Z","updated_at":"2025-05-16T12:02:23.364Z","avatar_url":"https://github.com/nisaacson.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","JAVASCRIPT"],"sub_categories":[],"readme":"# Node PDF\nNode PDF is a set of tools that takes in PDF files and converts them to usable formats for data processing. The library supports both extracting text from searchable pdf files as well as performing OCR on pdfs which are just scanned images of text\n\n[![Build Status](https://travis-ci.org/nisaacson/pdf-extract.png)](https://travis-ci.org/nisaacson/pdf-extract)\n\n## Installation\n\nTo begin install the module.\n\n`npm install pdf-extract`\n\nAfter the library is installed you will need the following binaries accessible on your path to process pdfs.\n\n- pdftk\n    - pdftk splits multi-page pdf into single pages.\n- pdftotext\n    - pdftotext is used to extract text out of searchable pdf documents\n- ghostscript\n    - ghostscript is an ocr preprocessor which convert pdfs to tif files for input into tesseract\n- tesseract\n    - tesseract performs the actual ocr on your scanned images\n\n\n### OSX\nTo begin on OSX, first make sure you have the homebrew package manager installed.\n\n**pdftk** is not available in Homebrew. However a gui install is available here.\n[http://www.pdflabs.com/docs/install-pdftk/](http://www.pdflabs.com/docs/install-pdftk/)\n\n**pdftotext** is included as part of the **poppler** utilities library. **poppler** can be installed via homebrew\n\n``` bash\nbrew install poppler\n```\n\n**ghostscript** can be install via homebrew\n``` bash\nbrew install gs\n```\n\n**tesseract** can be installed via homebrew as well\n\n``` bash\nbrew install tesseract\n```\n\nAfter tesseract is installed you need to install the alphanumeric config and an updated trained data file\n``` bash\ncd \u003croot of this module\u003e\ncp \"./share/eng.traineddata\" \"/usr/local/Cellar/tesseract/3.02.02_3/share/tessdata/eng.traineddata\"\ncp \"./share/dia.traineddata\" \"/usr/local/Cellar/tesseract/3.02.02_3/share/tessdata/dia.traineddata\"\ncp \"./share/configs/alphanumeric\" \"/usr/local/Cellar/tesseract/3.02.02_3/share/tessdata/configs/alphanumeric\"\n```\n\n### Ubuntu\n**pdftk** can be installed directly via apt-get\n```bash\napt-get install pdftk\n```\n\n**pdftotext** is included in the **poppler-utils** library. To installer poppler-utils execute\n``` bash\napt-get install poppler-utils\n```\n\n**ghostscript** can be install via apt-get\n``` bash\napt-get install ghostscript\n```\n\n**tesseract** can be installed via apt-get. Note that unlike the osx install the package is called **tesseract-ocr** on Ubuntu, not **tesseract**\n``` bash\napt-get install tesseract-ocr\n```\n\nFor the OCR to work, you need to have the tesseract-ocr binaries available on your path. If you only need to handle ASCII characters, the accuracy of the OCR process can be increased by limiting the tesseract output. To do this copy the *alphanumeric* file included with this pdf-extract module into the *tess-data* folder on your system. Also the eng.traineddata included with the standard tesseract-ocr package is out of date. This pdf-extract module provides an up-to-date version which you should copy into the appropriate location on your system\n``` bash\ncd \u003croot of this module\u003e\ncp \"./share/eng.traineddata\" \"/usr/share/tesseract-ocr/tessdata/eng.traineddata\"\ncp \"./share/configs/alphanumeric\" \"/usr/share/tesseract-ocr/tessdata/configs/alphanumeric\"\n```\n\n\n### SmartOS\n**pdftk** can be installed directly via apt-get\n``` bash\napt-get install pdftk\n```\n\n**pdftotext** is included in the **poppler-utils** library. To installer poppler-utils execute\n``` bash\napt-get install poppler-utils\n```\n\n**ghostscript** can be install via pkgin. Note you may need to update the pkgin repo to include the additional sources provided by Joyent. Check [http://www.perkin.org.uk/posts/9000-packages-for-smartos-and-illumos.html](http://www.perkin.org.uk/posts/9000-packages-for-smartos-and-illumos.html) for details\n``` bash\npkgin install ghostscript\n```\n\n**tesseract** can be must be manually downloaded and compiled. You must also install leptonica before installing tesseract. At the time of this writing leptonica is available from [http://www.leptonica.com/download.html](http://www.leptonica.com/download.html), with the latest version tarball available from [http://www.leptonica.com/source/leptonica-1.69.tar.gz](http://www.leptonica.com/source/leptonica-1.69.tar.gz)\n``` bash\npkgin install autoconf\nwget http://www.leptonica.com/source/leptonica-1.69.tar.gz\ntar -xvzf leptonica-1.69.tar.gz\ncd leptonica-1.69\n./configure\nmake\n[sudo] make install\n```\nAfter installing leptonic move on to tesseract. Tesseract is available from [https://code.google.com/p/tesseract-ocr/downloads/list](https://code.google.com/p/tesseract-ocr/downloads/list) with the latest version available from [https://code.google.com/p/tesseract-ocr/downloads/detail?name=tesseract-ocr-3.02.02.tar.gz\u0026can=2\u0026q=](https://code.google.com/p/tesseract-ocr/downloads/detail?name=tesseract-ocr-3.02.02.tar.gz\u0026can=2\u0026q=)\n``` bash\nwget https://code.google.com/p/tesseract-ocr/downloads/detail?name=tesseract-ocr-3.02.02.tar.gz\u0026can=2\u0026q=\ntar -xvzf tesseract-ocr-3.02.02.tar.gz\ncd tesseract-ocr\n./configure\nmake\n[sudo] make install\n```\n\n### Windows\nImportant! You will have to add some variables to the PATH of your machine. You do this by right clicking your computer in file explorer, select Properties, select Advanced System Settings, Environment Variables. You can then add **the folder that contains the executables** to the path variable.\n\n**pdftk** can be installed using the PDFtk Server installer found here: https://www.pdflabs.com/tools/pdftk-server/\nIt should autmatically add itself to the PATH, if not, the default install location is *\"C:\\Program Files (x86)\\PDFtk Server\\bin\\\"*\n\n**pdftotext** can be installed using the recompiled poppler utils for windows, which have been collected and bundled here: http://manifestwebdesign.com/2013/01/09/xpdf-and-poppler-utils-on-windows/\nUnpack these in a folder, (example: *\"C:\\poppler-utils\"*) and add this to the PATH.\n\n**ghostscript** for Windows can be found at: http://www.ghostscript.com/download/gsdnld.html\nMake sure you download the General Public License and the correct version (32/64bit).\nInstall it and go to the installation folder (default: *\"C:\\Program Files\\gs\\gs9.19\"*) and go into the **bin** folder.\nRename the *gswin64c* to *gs*, and add the bin folder to your PATH.\n\n**tesseract** can be build, but you can also download an older version which seems to work fine. Downloads at: https://sourceforge.net/projects/tesseract-ocr-alt/files/\nVersion tested is *tesseract-ocr-setup-3.02.02.exe*, the default install location is *\"C:\\Program Files (x86)\\Tesseract-OCR\"* and is also added to the PATH.\nNote, this is only when you've checked that it will install for everyone on the machine.\n\nEverything should work after all this! If not, try restarting to make sure the PATH variables are correctly used.\n**This setup was tested on a Windows 10 Pro N 64bit machine.**\n\n\n## Usage\n\n### OCR Extract from scanned image\nExtract from a pdf file which contains a scanned image and no searchable text\n``` javascript\nconst path = require(\"path\")\nconst pdf_extract = require('pdf-extract')\n\nconsole.log(\"Usage: node thisfile.js the/path/tothe.pdf\")\nconst absolute_path_to_pdf = path.resolve(process.argv[2])\nif (absolute_path_to_pdf.includes(\" \")) throw new Error(\"will fail for paths w spaces like \"+absolute_path_to_pdf)\n\nconst options = {\n  type: 'ocr', // perform ocr to get the text within the scanned image\n  ocr_flags: ['--psm 1'], // automatically detect page orientation\n}\nconst processor = pdf_extract(absolute_path_to_pdf, options, ()=\u003econsole.log(\"Starting…\"))\nprocessor.on('complete', data =\u003e callback(null, data))\nprocessor.on('error', callback)\nfunction callback (error, data) { error ? console.error(error) : console.log(data.text_pages[0]) }\n```\n\n\n\n### Text extract from searchable pdf\nExtract from a pdf file which contains actual searchable text\n``` javascript\nconst path = require(\"path\")\nconst pdf_extract = require('./main.js')\n\nconsole.log(\"Usage: node thisfile.js the/path/tothe.pdf\")\nconst absolute_path_to_pdf = path.resolve(process.argv[2])\nif (absolute_path_to_pdf.includes(\" \")) throw new Error(\"will fail for paths w spaces like \"+absolute_path_to_pdf)\n\nconst options = {\n  type: 'text', // extract searchable text from PDF\n  ocr_flags: ['--psm 1'], // automatically detect page orientation\n  enc: 'UTF-8',  // optional, encoding to use for the text output\n  mode: 'layout' // optional, mode to use when reading the pdf\n}\nconst processor = pdf_extract(absolute_path_to_pdf, options, ()=\u003econsole.log(\"Starting…\"))\nprocessor.on('complete', data =\u003e callback(null, data))\nprocessor.on('error', callback)\nfunction callback (error, data) { error ? console.error(error) : console.log(data.text_pages[0]) }\n```\n#### Options\nAt a minimum you must specific the type of pdf extract you wish to perform\n\n**clean**\nWhen the system performs extracts text from a multi-page pdf, it first splits the pdf into single pages. This are written to disk before the ocr occurs. For some applications these single page files can be useful. If you need to work with the single page pdf files after the ocr is complete, set the **clean** option to **false** as show below. Note that the single page pdf files are written to the system appropriate temp directory, so if you must copy the files to a more permanent location yourself after the ocr process completes\n``` javascript\nvar options = {\n  type: 'ocr' // (required), perform ocr to get the text within the scanned image\n  enc: 'UTF-8' // optional, only applies to 'text' type\n  mode: 'layout' // optional, only applies to 'text' type. Available modes are 'layout', 'simple', 'table' or 'lineprinter'. Default is 'layout'\n  clean: false // keep the single page pdfs created during the ocr process\n  ocr_flags: [\n    '-psm 1',       // automatically detect page orientation\n    '-l dia',       // use a custom language file\n    'alphanumeric'  // only output ascii characters\n  ]\n}\n```\n\n\n### Events\nWhen processing, the module will emit various events as they occurr\n\n**page**\nEmitted when a page has completed processing. The data passed with this event looks like\n``` javascript\nvar data = {\n  hash: \u003csha1 hash of the input pdf file here\u003e\n  text: \u003cextracted text here\u003e,\n  index: 2,\n  num_pages: 4,\n  pdf_path: \"~/Downloads/input_pdf_file.pdf\",\n  single_page_pdf_path: \"/tmp/temp_pdf_file2.pdf\"\n}\n```\n\n**error**\nEmitted when an error occurs during processing. After this event is emitted processing will stop.\nThe data passed with this event looks like\n```\nvar data = {\n  error: 'no file exists at the path you specified',\n  pdf_path: \"~/Downloads/input_pdf_file.pdf\",\n}\n```\n\n**complete**\nEmitted when all pages have completed processing and the pdf extraction is complete\n```\nvar data = {\n  hash: \u003csha1 hash of the input pdf file here\u003e\n  text_pages: \u003cArray of Strings, one per page\u003e,\n  pdf_path: \"~/Downloads/input_pdf_file.pdf\",\n  single_page_pdf_file_paths: [\n    \"/tmp/temp_pdf_file1.pdf\",\n    \"/tmp/temp_pdf_file2.pdf\",\n    \"/tmp/temp_pdf_file3.pdf\",\n    \"/tmp/temp_pdf_file4.pdf\",\n  ]\n}\n```\n\n**log**\nTo avoid spamming process.stdout, log events are emitted instead.\n\n## Tests\nTo test that your system satisfies the needed dependencies and that module is functioning correctly execute the command in the pdf-extract module folder\n```\ncd \u003cproject_root\u003e/node_modules/pdf-extract\nnpm test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnisaacson%2Fpdf-extract","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnisaacson%2Fpdf-extract","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnisaacson%2Fpdf-extract/lists"}