Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Anything Llm
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
MachineLearning
Mintplex Labs
Anything Llm
Commits
9b86bbd2
Unverified
Commit
9b86bbd2
authored
8 months ago
by
Sean Hatfield
Committed by
GitHub
8 months ago
Browse files
Options
Downloads
Patches
Plain Diff
[FIX] PDFLoader module bug fix (#1879)
use pdf.js by importing it from pdf-parse and fix custom PDFLoader module
parent
86a66ba5
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
collector/processSingleFile/convert/asPDF/PDFLoader/index.js
+75
-40
75 additions, 40 deletions
collector/processSingleFile/convert/asPDF/PDFLoader/index.js
with
75 additions
and
40 deletions
collector/processSingleFile/convert/asPDF/PDFLoader/index.js
+
75
−
40
View file @
9b86bbd2
const
fs
=
require
(
"
fs
"
).
promises
;
const
pdf
=
require
(
"
pdf-parse
"
);
class
PDFLoader
{
constructor
(
filePath
,
{
splitPages
=
true
}
=
{})
{
...
...
@@ -9,54 +8,90 @@ class PDFLoader {
async
load
()
{
const
buffer
=
await
fs
.
readFile
(
this
.
filePath
);
const
{
getDocument
,
version
}
=
await
this
.
getPdfJS
();
const
options
=
{
pagerender
:
this
.
splitPages
?
this
.
renderPage
:
null
,
};
const
{
text
,
numpages
,
info
,
metadata
,
version
}
=
await
pdf
(
buffer
,
options
);
if
(
!
this
.
splitPages
)
{
return
[
{
pageContent
:
text
.
trim
(),
metadata
:
{
source
:
this
.
filePath
,
pdf
:
{
version
,
info
,
metadata
,
totalPages
:
numpages
},
const
pdf
=
await
getDocument
({
data
:
new
Uint8Array
(
buffer
),
useWorkerFetch
:
false
,
isEvalSupported
:
false
,
useSystemFonts
:
true
,
}).
promise
;
const
meta
=
await
pdf
.
getMetadata
().
catch
(()
=>
null
);
const
documents
=
[];
for
(
let
i
=
1
;
i
<=
pdf
.
numPages
;
i
+=
1
)
{
const
page
=
await
pdf
.
getPage
(
i
);
const
content
=
await
page
.
getTextContent
();
if
(
content
.
items
.
length
===
0
)
{
continue
;
}
let
lastY
;
const
textItems
=
[];
for
(
const
item
of
content
.
items
)
{
if
(
"
str
"
in
item
)
{
if
(
lastY
===
item
.
transform
[
5
]
||
!
lastY
)
{
textItems
.
push
(
item
.
str
);
}
else
{
textItems
.
push
(
`\n
${
item
.
str
}
`
);
}
lastY
=
item
.
transform
[
5
];
}
}
const
text
=
textItems
.
join
(
""
);
documents
.
push
({
pageContent
:
text
.
trim
(),
metadata
:
{
source
:
this
.
filePath
,
pdf
:
{
version
,
info
:
meta
?.
info
,
metadata
:
meta
?.
metadata
,
totalPages
:
pdf
.
numPages
,
},
loc
:
{
pageNumber
:
i
},
},
];
});
}
if
(
this
.
splitPages
)
{
return
documents
;
}
if
(
documents
.
length
===
0
)
{
return
[];
}
return
this
.
pages
.
map
((
pageContent
,
index
)
=>
({
pageContent
:
pageContent
.
trim
(),
metadata
:
{
source
:
this
.
filePath
,
pdf
:
{
version
,
info
,
metadata
,
totalPages
:
numpages
},
loc
:
{
pageNumber
:
index
+
1
},
return
[
{
pageContent
:
documents
.
map
((
doc
)
=>
doc
.
pageContent
).
join
(
"
\n\n
"
),
metadata
:
{
source
:
this
.
filePath
,
pdf
:
{
version
,
info
:
meta
?.
info
,
metadata
:
meta
?.
metadata
,
totalPages
:
pdf
.
numPages
,
},
},
},
}))
;
]
;
}
pages
=
[];
renderPage
=
async
(
pageData
)
=>
{
const
textContent
=
await
pageData
.
getTextContent
();
let
lastY
,
text
=
""
;
for
(
const
item
of
textContent
.
items
)
{
if
(
lastY
!==
item
.
transform
[
5
]
&&
lastY
!==
undefined
)
{
text
+=
"
\n
"
;
}
text
+=
item
.
str
;
lastY
=
item
.
transform
[
5
];
async
getPdfJS
()
{
try
{
const
pdfjs
=
await
import
(
"
pdf-parse/lib/pdf.js/v1.10.100/build/pdf.js
"
);
return
{
getDocument
:
pdfjs
.
getDocument
,
version
:
pdfjs
.
version
};
}
catch
(
e
)
{
console
.
error
(
e
);
throw
new
Error
(
"
Failed to load pdf-parse. Please install it with eg. `npm install pdf-parse`.
"
);
}
this
.
pages
.
push
(
text
);
return
text
;
};
}
}
module
.
exports
=
PDFLoader
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment