Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
L
Lib-PDF
Manage
Activity
Members
Labels
Plan
Issues
2
Issue boards
Milestones
Wiki
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository 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
Code
JS Toolbox
Lib-PDF
Commits
52a1720d
Commit
52a1720d
authored
3 years ago
by
Zdravko Iliev
Browse files
Options
Downloads
Patches
Plain Diff
scale down the qrcode and insert link
parent
60b08df6
No related branches found
No related tags found
1 merge request
!1
Draft: Resolve "[Document Sealing] Implement PDF parser"
Pipeline
#49113
passed with stages
in 49 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
dist/pdfParser.d.ts
+1
-0
1 addition, 0 deletions
dist/pdfParser.d.ts
dist/pdfParser.js
+23
-12
23 additions, 12 deletions
dist/pdfParser.js
src/pdfParser.ts
+38
-14
38 additions, 14 deletions
src/pdfParser.ts
with
62 additions
and
26 deletions
dist/pdfParser.d.ts
+
1
−
0
View file @
52a1720d
...
...
@@ -6,5 +6,6 @@ declare class PDFparser {
constructor
(
document
:
Buffer
);
getPDFMeta
:
()
=>
Promise
<
IgetMetaResponse
>
;
insertQrCode
:
(
imgBytes
:
ArrayBuffer
)
=>
Promise
<
ArrayBuffer
>
;
private
createPageLinkAnnotation
;
}
export
default
PDFparser
;
This diff is collapsed.
Click to expand it.
dist/pdfParser.js
+
23
−
12
View file @
52a1720d
...
...
@@ -13,6 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object
.
defineProperty
(
exports
,
"
__esModule
"
,
{
value
:
true
});
const
verify_pdf_1
=
__importDefault
(
require
(
"
@ninja-labs/verify-pdf
"
));
const
pdf_lib_1
=
require
(
"
pdf-lib
"
);
const
pdfdataextract_1
=
require
(
"
pdfdataextract
"
);
const
config_1
=
require
(
"
./config
"
);
const
{
PDFDocument
}
=
require
(
"
pdf-lib
"
);
...
...
@@ -48,25 +49,35 @@ class PDFparser {
this
.
insertQrCode
=
(
imgBytes
)
=>
__awaiter
(
this
,
void
0
,
void
0
,
function
*
()
{
const
pdfDoc
=
yield
PDFDocument
.
load
(
this
.
document
);
const
img
=
yield
pdfDoc
.
embedPng
(
imgBytes
);
//
const
imagePage = pdfDoc.insertPag
e(0);
const
scaled
=
img
.
scal
e
(
0
.5
);
const
pages
=
pdfDoc
.
getPages
();
const
firstPage
=
pages
[
0
];
const
{
width
,
height
}
=
firstPage
.
getSize
();
firstPage
.
drawImage
(
img
,
{
x
:
firstPage
.
getWidth
()
/
2
-
img
.
width
/
2
,
y
:
firstPage
.
getHeight
()
/
2
-
img
.
height
/
2
,
width
:
img
.
width
,
height
:
img
.
height
,
x
:
firstPage
.
getWidth
()
/
2
-
scaled
.
width
/
2
,
y
:
firstPage
.
getHeight
()
/
2
-
scaled
.
height
/
2
,
width
:
scaled
.
width
,
height
:
scaled
.
height
,
});
// firstPage.drawImage(img, {
// x: 0,
// y: 0,
// width: firstPage.getWidth(),
// height: firstPage.getHeight(),
// });
const
link
=
this
.
createPageLinkAnnotation
(
firstPage
,
"
https://pdf-lib.js.org/
"
,
{
imgXPos
:
firstPage
.
getWidth
()
/
2
-
scaled
.
width
/
2
,
imgYPos
:
firstPage
.
getHeight
()
/
2
-
scaled
.
height
/
2
,
imgWidth
:
scaled
.
width
,
imagHeight
:
scaled
.
height
,
});
firstPage
.
node
.
set
(
pdf_lib_1
.
PDFName
.
of
(
"
Annots
"
),
pdfDoc
.
context
.
obj
([
link
]));
const
pdfBytes
=
yield
pdfDoc
.
save
();
return
pdfBytes
;
});
this
.
createPageLinkAnnotation
=
(
page
,
uri
,
{
imgXPos
,
imgYPos
,
imgWidth
,
imagHeight
})
=>
page
.
doc
.
context
.
register
(
page
.
doc
.
context
.
obj
({
Type
:
"
Annot
"
,
Subtype
:
"
Link
"
,
Rect
:
[
imgXPos
,
imgYPos
,
imgXPos
+
imgWidth
,
imgYPos
+
imagHeight
],
A
:
{
Type
:
"
Action
"
,
S
:
"
URI
"
,
URI
:
pdf_lib_1
.
PDFString
.
of
(
uri
),
},
}));
this
.
document
=
document
;
this
.
config
=
config_1
.
config
;
}
...
...
This diff is collapsed.
Click to expand it.
src/pdfParser.ts
+
38
−
14
View file @
52a1720d
import
verifyPDF
from
"
@ninja-labs/verify-pdf
"
;
import
{
PDFName
,
PDFPage
,
PDFString
}
from
"
pdf-lib
"
;
import
{
PdfData
}
from
"
pdfdataextract
"
;
import
{
config
}
from
"
./config
"
;
import
{
IgetMetaResponse
}
from
"
./types
"
;
const
{
PDFDocument
}
=
require
(
"
pdf-lib
"
);
class
PDFparser
{
readonly
document
;
readonly
config
;
...
...
@@ -44,31 +44,55 @@ class PDFparser {
insertQrCode
=
async
(
imgBytes
:
ArrayBuffer
):
Promise
<
ArrayBuffer
>
=>
{
const
pdfDoc
=
await
PDFDocument
.
load
(
this
.
document
);
const
img
=
await
pdfDoc
.
embedPng
(
imgBytes
);
// const imagePage = pdfDoc.insertPage(0);
const
scaled
=
img
.
scale
(
0.5
);
const
pages
=
pdfDoc
.
getPages
();
const
firstPage
=
pages
[
0
];
const
{
width
,
height
}
=
firstPage
.
getSize
()
;
const
firstPage
=
pages
[
0
]
;
firstPage
.
drawImage
(
img
,
{
x
:
firstPage
.
getWidth
()
/
2
-
img
.
width
/
2
,
y
:
firstPage
.
getHeight
()
/
2
-
img
.
height
/
2
,
width
:
img
.
width
,
height
:
img
.
height
,
x
:
firstPage
.
getWidth
()
/
2
-
scaled
.
width
/
2
,
y
:
firstPage
.
getHeight
()
/
2
-
scaled
.
height
/
2
,
width
:
scaled
.
width
,
height
:
scaled
.
height
,
});
// firstPage.drawImage(img, {
// x: 0,
// y: 0,
// width: firstPage.getWidth(),
// height: firstPage.getHeight(),
// });
const
link
=
this
.
createPageLinkAnnotation
(
firstPage
,
"
https://pdf-lib.js.org/
"
,
{
imgXPos
:
firstPage
.
getWidth
()
/
2
-
scaled
.
width
/
2
,
imgYPos
:
firstPage
.
getHeight
()
/
2
-
scaled
.
height
/
2
,
imgWidth
:
scaled
.
width
,
imagHeight
:
scaled
.
height
,
}
);
firstPage
.
node
.
set
(
PDFName
.
of
(
"
Annots
"
),
pdfDoc
.
context
.
obj
([
link
]));
const
pdfBytes
=
await
pdfDoc
.
save
();
return
pdfBytes
;
};
private
createPageLinkAnnotation
=
(
page
:
PDFPage
,
uri
:
string
,
{
imgXPos
,
imgYPos
,
imgWidth
,
imagHeight
}
)
=>
page
.
doc
.
context
.
register
(
page
.
doc
.
context
.
obj
({
Type
:
"
Annot
"
,
Subtype
:
"
Link
"
,
Rect
:
[
imgXPos
,
imgYPos
,
imgXPos
+
imgWidth
,
imgYPos
+
imagHeight
],
A
:
{
Type
:
"
Action
"
,
S
:
"
URI
"
,
URI
:
PDFString
.
of
(
uri
),
},
})
);
}
export
default
PDFparser
;
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