This commit is contained in:
2026-10-08 18:18:21 +08:00
parent 9cc1ff546c
commit 1e3095c05e
1911 changed files with 349551 additions and 0 deletions
@@ -0,0 +1,12 @@
root = true
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.yml]
indent_style = space
indent_size = 2
@@ -0,0 +1,94 @@
#image: flightstats/nightmare-runner
#image: aheuermann/electron:latest
image: node:8.9.3
stages:
- install
- test
- version
- publish
- package
before_script:
- git config --global user.email $GITLAB_USER_EMAIL
- git config --global user.name $GITLAB_USER_LOGIN
- apt-get update -qq
- apt-get install -qq -y xvfb libgtk2.0-0 libnotify4 libgconf-2-4 libnss3 libxss1
#- apt-get install -y xvfb libgtk2.0-0 libnotify4 libgconf-2-4 libnss3 libxss1 x11-xkb-utils xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic x11-apps clang wget libgtk2.0 libasound2 libcups2 libxss1 libnss3-dev gcc-multilib
- Xvfb :99 -screen 0 1024x768x24 +extension RANDR > /dev/null 2>&1 &
- export DISPLAY=':99.0'
#- Xvfb -ac -screen scrn 1280x2000x24 :9.0 &
#- export DISPLAY=:9.0
after_script:
#- git commit -a -q -m "ci output added"
#- git push ${CI_REPOSITORY_URL//$CI_REGISTRY_PASSWORD/$CI_ACCESS_TOKEN} master
- echo "end of script"
package_install:
stage: install
script:
- pwd
- npm install --save --save-dev --save-exact
mocha_test:
stage: test
script:
- pwd
- npm install --save --save-dev --save-exact
- npm run test
#- git commit -a -q -m "ci output added" ${CI_REPOSITORY_URL//$CI_REGISTRY_PASSWORD/$CI_ACCESS_TOKEN} &>/dev/null
version_check:
stage: version
only:
- master
script:
- pwd
- npm install --save --save-dev --save-exact
- npm run test
- REPO_VERSION=$(npm view $CI_PROJECT_NAME version)
- CURRENT_VERSION=$(node -p "require('./package.json').version")
- |
if [ "${REPO_VERSION}" != "${CURRENT_VERSION}" ]; then
echo "repo version not equal to current version"
echo REPO_VERSION=${REPO_VERSION}
echo CURRENT_VERSION=${CURRENT_VERSION}
git tag v${CURRENT_VERSION} master
git push ${CI_REPOSITORY_URL//$CI_REGISTRY_PASSWORD/$CI_ACCESS_TOKEN} v${CURRENT_VERSION} -q &>/dev/null
fi
npm_publish:
stage: publish
only:
- tags
script:
- pwd
- echo //registry.npmjs.org/:_authToken=${NPM_TOKEN} > ~/.npmrc
- npm publish
package_test:
stage: package
only:
- tags
script:
- pwd
- cd ..
- rm -rf npm_package_install
- mkdir npm_package_install
- cd npm_package_install
- npm install --prefix ./ ${CI_PROJECT_NAME}
- cd node_modules
- cd ${CI_PROJECT_NAME}
- REPO_VERSION=$(npm view ${CI_PROJECT_NAME} version)
- CURRENT_VERSION=$(node -p "require('./package.json').version")
- |
if [ "${REPO_VERSION}" == "${CURRENT_VERSION}" ]; then
echo "repo version equal to current version"
echo REPO_VERSION=${REPO_VERSION}
echo CURRENT_VERSION=${CURRENT_VERSION}
npm install --save --save-dev --save-exact
npm run test
else
exit 1
fi
@@ -0,0 +1,17 @@
{
"indent_size": 4,
"indent_char": " ",
"css": {
"indent_size": 2
},
"beautify.language": {
"js": {
"type": ["javascript", "json"],
"filename": [".jshintrc", ".jsbeautifyrc"],
"ext": ["js", "json"]
},
"css": ["css", "scss"],
"html": ["htm", "html"]
}
}
@@ -0,0 +1,38 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/index.js",
"env": {
"DEBUG": "autokent:*,-not_this"
}
},
{
"type": "node",
"request": "launch",
"name": "Launch QuickStart",
"program": "${workspaceFolder}/quickstart.js"
},
{
"type": "node",
"request": "launch",
"name": "Launch Test",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--recursive",
"--slow",
"20000"
],
"cwd": "${workspaceRoot}",
"env": {
"DEBUG": "autokent:*,-not_this"
}
}
]
}
@@ -0,0 +1,20 @@
{
"cSpell.words": [
"PDFJS",
"PDFs",
"QUICKSTART",
"behaviour",
"catchable",
"endothelium",
"gettype",
"numpages",
"numrender",
"pagerender",
"pagerendered",
"parsedpages",
"pdfjson",
"pdfreader",
"simplecrawler",
"xpdf"
]
}
+28
View File
@@ -0,0 +1,28 @@
# 1.1.1
* pdf.js new versions added.
* default stable version changed v1.9.426 to v1.10.100
* https://mozilla.github.io/pdf.js/getting_started/#download
* fixed issue [#5](https://gitlab.com/autokent/pdf-parse/issues/5)
# 1.1.0
* readme updated
* custom callback must return current page text not all page's text [#1 commment](https://gitlab.com/autokent/pdf-parse/issues/4#note_111256593)
# 1.0.9
* extra space injection issue fixed. [#1](https://gitlab.com/autokent/pdf-parse/issues/4)
* custom callback signature changed.
# 1.0.8
* co dependency removed.
# 1.0.5 - 1.0.7
* test cases updated.
# 1.0.2 - 1.0.4
* readme updated.
# 1.0.1
* added 70+ unit test cases.
# 0.0.1
* initial release
@@ -0,0 +1,12 @@
# Contributing
> Developed using `Visual Studio Code` Version 1.8.1
If you are thinking about making better, or you just want to hack on it, that’s great!
If you find a bug in the source code or a mistake in the documentation, you can help us by submitting an issue to GitLab Repository. Even better you can submit a Merge Request with a fix.
**Working on your first Merge Request?** You can learn how from this *free* series
[How to Contribute to an Open Source Project on GitLab][gitlab_first_time]
[gitlab_first_time]: https://about.gitlab.com/2016/06/16/fearless-contribution-a-guide-for-first-timers/
+21
View File
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2017 Mehmet Kozan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+1
View File
@@ -0,0 +1 @@
# notes
@@ -0,0 +1,14 @@
const PDF = require('./');
const fs = require('fs');
let PDF_FILE = './test/data/01-valid.pdf';
let dataBuffer = fs.readFileSync('./test/data/01-valid.pdf');
PDF(dataBuffer).then(function(data) {
fs.writeFileSync(`${PDF_FILE}.txt`, data.text, {
encoding: 'utf8',
flag: 'w'
});
debugger;
}).catch(function(err) {
debugger;
});
+160
View File
@@ -0,0 +1,160 @@
# pdf-parse
**Pure javascript cross-platform module to extract texts from PDFs.**
[![version](https://img.shields.io/npm/v/pdf-parse.svg)](https://www.npmjs.org/package/pdf-parse)
[![downloads](https://img.shields.io/npm/dt/pdf-parse.svg)](https://www.npmjs.org/package/pdf-parse)
[![node](https://img.shields.io/node/v/pdf-parse.svg)](https://nodejs.org/)
[![status](https://gitlab.com/autokent/pdf-parse/badges/master/pipeline.svg)](https://gitlab.com/autokent/pdf-parse/pipelines)
## Similar Packages
* [pdf2json](https://www.npmjs.com/package/pdf2json) buggy, no support anymore, memory leak, throws non-catchable fatal errors
* [j-pdfjson](https://www.npmjs.com/package/j-pdfjson) fork of pdf2json
* [pdf-parser](https://github.com/dunso/pdf-parse) buggy, no tests
* [pdfreader](https://www.npmjs.com/package/pdfreader) using pdf2json
* [pdf-extract](https://www.npmjs.com/package/pdf-extract) not cross-platform using xpdf
## Installation
`npm install pdf-parse`
## Basic Usage - Local Files
```js
const fs = require('fs');
const pdf = require('pdf-parse');
let dataBuffer = fs.readFileSync('path to PDF file...');
pdf(dataBuffer).then(function(data) {
// number of pages
console.log(data.numpages);
// number of rendered pages
console.log(data.numrender);
// PDF info
console.log(data.info);
// PDF metadata
console.log(data.metadata);
// PDF.js version
// check https://mozilla.github.io/pdf.js/getting_started/
console.log(data.version);
// PDF text
console.log(data.text);
});
```
## Basic Usage - HTTP
You can use [crawler-request](https://www.npmjs.com/package/crawler-request) which uses the `pdf-parse`
## Exception Handling
```js
const fs = require('fs');
const pdf = require('pdf-parse');
let dataBuffer = fs.readFileSync('path to PDF file...');
pdf(dataBuffer).then(function(data) {
// use data
})
.catch(function(error){
// handle exceptions
})
```
## Extend
* v1.0.9 and above break pagerender callback [changelog](https://gitlab.com/autokent/pdf-parse/blob/master/CHANGELOG)
* If you need another format like json, you can change page render behaviour with a callback
* Check out https://mozilla.github.io/pdf.js/
```js
// default render callback
function render_page(pageData) {
//check documents https://mozilla.github.io/pdf.js/
let render_options = {
//replaces all occurrences of whitespace with standard spaces (0x20). The default value is `false`.
normalizeWhitespace: false,
//do not attempt to combine same line TextItem's. The default value is `false`.
disableCombineTextItems: false
}
return pageData.getTextContent(render_options)
.then(function(textContent) {
let lastY, text = '';
for (let item of textContent.items) {
if (lastY == item.transform[5] || !lastY){
text += item.str;
}
else{
text += '\n' + item.str;
}
lastY = item.transform[5];
}
return text;
});
}
let options = {
pagerender: render_page
}
let dataBuffer = fs.readFileSync('path to PDF file...');
pdf(dataBuffer,options).then(function(data) {
//use new format
});
```
## Options
```js
const DEFAULT_OPTIONS = {
// internal page parser callback
// you can set this option, if you need another format except raw text
pagerender: render_page,
// max page number to parse
max: 0,
//check https://mozilla.github.io/pdf.js/getting_started/
version: 'v1.10.100'
}
```
### *pagerender* (callback)
If you need another format except raw text.
### *max* (number)
Max number of page to parse. If the value is less than or equal to 0, parser renders all pages.
### *version* (string, pdf.js version)
check [pdf.js](https://mozilla.github.io/pdf.js/getting_started/)
* `'default'`
* `'v1.9.426'`
* `'v1.10.100'`
* `'v1.10.88'`
* `'v2.0.550'`
>*default* uses version *v1.10.100*
>[mozilla.github.io/pdf.js](https://mozilla.github.io/pdf.js/getting_started/#download)
## Test
* `mocha` or `npm test`
* Check [test folder](https://gitlab.com/autokent/pdf-parse/tree/master/test) and [quickstart.js](https://gitlab.com/autokent/pdf-parse/blob/master/quickstart.js) for extra usages.
## Support
I use this package actively myself, so it has my top priority. You can chat on WhatsApp about any infos, ideas and suggestions.
[![WhatsApp](https://img.shields.io/badge/style-chat-green.svg?style=flat&label=whatsapp)](https://api.whatsapp.com/send?phone=905063042480&text=Hi%2C%0ALet%27s%20talk%20about%20pdf-parse)
### Submitting an Issue
If you find a bug or a mistake, you can help by submitting an issue to [GitLab Repository](https://gitlab.com/autokent/pdf-parse/issues)
### Creating a Merge Request
GitLab calls it merge request instead of pull request.
* [A Guide for First-Timers](https://about.gitlab.com/2016/06/16/fearless-contribution-a-guide-for-first-timers/)
* [How to create a merge request](https://docs.gitlab.com/ee/gitlab-basics/add-merge-request.html)
* Check [Contributing Guide](https://gitlab.com/autokent/pdf-parse/blob/master/CONTRIBUTING.md)
## License
[MIT licensed](https://gitlab.com/autokent/pdf-parse/blob/master/LICENSE) and all it's dependencies are MIT or BSD licensed.
+26
View File
@@ -0,0 +1,26 @@
const Fs = require('fs');
const Pdf = require('./lib/pdf-parse.js');
module.exports = Pdf;
let isDebugMode = !module.parent;
//process.env.AUTO_KENT_DEBUG
//for testing purpose
if (isDebugMode) {
let PDF_FILE = './test/data/05-versions-space.pdf';
let dataBuffer = Fs.readFileSync(PDF_FILE);
Pdf(dataBuffer).then(function(data) {
Fs.writeFileSync(`${PDF_FILE}.txt`, data.text, {
encoding: 'utf8',
flag: 'w'
});
debugger;
}).catch(function(err) {
debugger;
});
}
@@ -0,0 +1,102 @@
var PDFJS = null;
function render_page(pageData) {
//check documents https://mozilla.github.io/pdf.js/
//ret.text = ret.text ? ret.text : "";
let render_options = {
//replaces all occurrences of whitespace with standard spaces (0x20). The default value is `false`.
normalizeWhitespace: false,
//do not attempt to combine same line TextItem's. The default value is `false`.
disableCombineTextItems: false
}
return pageData.getTextContent(render_options)
.then(function(textContent) {
let lastY, text = '';
//https://github.com/mozilla/pdf.js/issues/8963
//https://github.com/mozilla/pdf.js/issues/2140
//https://gist.github.com/hubgit/600ec0c224481e910d2a0f883a7b98e3
//https://gist.github.com/hubgit/600ec0c224481e910d2a0f883a7b98e3
for (let item of textContent.items) {
if (lastY == item.transform[5] || !lastY){
text += item.str;
}
else{
text += '\n' + item.str;
}
lastY = item.transform[5];
}
//let strings = textContent.items.map(item => item.str);
//let text = strings.join("\n");
//text = text.replace(/[ ]+/ig," ");
//ret.text = `${ret.text} ${text} \n\n`;
return text;
});
}
const DEFAULT_OPTIONS = {
pagerender: render_page,
max: 0,
//check https://mozilla.github.io/pdf.js/getting_started/
version: 'v1.10.100'
}
async function PDF(dataBuffer, options) {
var isDebugMode = false;
let ret = {
numpages: 0,
numrender: 0,
info: null,
metadata: null,
text: "",
version: null
};
if (typeof options == 'undefined') options = DEFAULT_OPTIONS;
if (typeof options.pagerender != 'function') options.pagerender = DEFAULT_OPTIONS.pagerender;
if (typeof options.max != 'number') options.max = DEFAULT_OPTIONS.max;
if (typeof options.version != 'string') options.version = DEFAULT_OPTIONS.version;
if (options.version == 'default') options.version = DEFAULT_OPTIONS.version;
PDFJS = PDFJS ? PDFJS : require(`./pdf.js/${options.version}/build/pdf.js`);
ret.version = PDFJS.version;
// Disable workers to avoid yet another cross-origin issue (workers need
// the URL of the script to be loaded, and dynamically loading a cross-origin
// script does not work).
PDFJS.disableWorker = true;
let doc = await PDFJS.getDocument(dataBuffer);
ret.numpages = doc.numPages;
let metaData = await doc.getMetadata().catch(function(err) {
return null;
});
ret.info = metaData ? metaData.info : null;
ret.metadata = metaData ? metaData.metadata : null;
let counter = options.max <= 0 ? doc.numPages : options.max;
counter = counter > doc.numPages ? doc.numPages : counter;
ret.text = "";
for (var i = 1; i <= counter; i++) {
let pageText = await doc.getPage(i).then(pageData => options.pagerender(pageData)).catch((err)=>{
// todo log err using debug
debugger;
return "";
});
ret.text = `${ret.text}\n\n${pageText}`;
}
ret.numrender = counter;
doc.destroy();
return ret;
}
module.exports = PDF;
@@ -0,0 +1,2 @@
# check https://mozilla.github.io/pdf.js/getting_started/#download
# just download then delete web folder and license file
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
@@ -0,0 +1,3 @@
àRCopyright 1990-2009 Adobe Systems Incorporated.
All rights reserved.
See ./LICENSEáCNS2-H
@@ -0,0 +1,3 @@
àRCopyright 1990-2009 Adobe Systems Incorporated.
All rights reserved.
See ./LICENSEá ETen-B5-H` ^
@@ -0,0 +1,4 @@
àRCopyright 1990-2009 Adobe Systems Incorporated.
All rights reserved.
See ./LICENSE!!�º]aX!!]`�21�> �p �z�$]‚�"R‚d�-Uƒ7�*„
4„%�+ „Z „{�/…%…<�9K…b�1]†.�"‡ ‰`]‡,�"]ˆ

Some files were not shown because too many files have changed in this diff Show More