Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
html2pdf
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Lukas Niegsch
html2pdf
Commits
689977b5
Commit
689977b5
authored
Jun 16, 2022
by
lukas.niegsch
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed printing and added default options
parent
ce1ae939
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
39 additions
and
8 deletions
+39
-8
chromium.js
chromium.js
+3
-3
connection.js
connection.js
+6
-3
index.html
index.html
+30
-2
No files found.
chromium.js
View file @
689977b5
...
@@ -105,10 +105,10 @@ class HeadlessChromium
...
@@ -105,10 +105,10 @@ class HeadlessChromium
return
await
this
.
runPageCommand
(
page
,
"
Page.enable
"
)
return
await
this
.
runPageCommand
(
page
,
"
Page.enable
"
)
}
}
async
printToPdf
(
page
)
async
printToPdf
(
page
,
options
)
{
{
// wip: printing fails for some reason
// wip: printing fails for some reason
var
response
=
await
this
.
runPageCommand
(
page
,
"
Page.printToPDF
"
)
var
response
=
await
this
.
runPageCommand
(
page
,
"
Page.printToPDF
"
,
options
)
return
response
return
response
.
data
//atob(response.data)
}
}
}
}
connection.js
View file @
689977b5
...
@@ -2,7 +2,7 @@ class RequestHandler
...
@@ -2,7 +2,7 @@ class RequestHandler
{
{
constructor
()
constructor
()
{
{
this
.
buffer
=
[
"
https://www.example.com
"
]
this
.
buffer
=
[
"
https://www.example.com
"
,
"
https://www.nexedi.com/
"
]
}
}
hasNextRequest
()
hasNextRequest
()
...
@@ -15,8 +15,11 @@ class RequestHandler
...
@@ -15,8 +15,11 @@ class RequestHandler
return
this
.
buffer
.
pop
()
return
this
.
buffer
.
pop
()
}
}
publishResults
(
data
)
publishResults
(
name
,
data
)
{
{
console
.
log
(
"
result:
\n
"
+
data
)
var
a
=
document
.
createElement
(
"
a
"
);
a
.
href
=
"
data:application/pdf;base64,
"
+
data
;
a
.
download
=
`
${
name
}
.pdf`
;
a
.
click
();
}
}
}
}
\ No newline at end of file
index.html
View file @
689977b5
...
@@ -7,6 +7,33 @@
...
@@ -7,6 +7,33 @@
<script
src=
"connection.js"
></script>
<script
src=
"connection.js"
></script>
<script
src=
"chromium.js"
></script>
<script
src=
"chromium.js"
></script>
<script>
<script>
function
mm2inches
(
mm
)
{
return
mm
/
25.4
}
/*
Options for printing:
https://chromedevtools.github.io/devtools-protocol/1-3/Page/#method-printToPDF
*/
var
optionsForPrintToPdf
=
{
landscape
:
true
,
displayHeaderFooter
:
false
,
printBackground
:
true
,
scale
:
1
,
paperWidth
:
mm2inches
(
297
/* millimetre */
),
paperHeight
:
mm2inches
(
210
/* millimetre */
),
marginTop
:
mm2inches
(
10
/* millimetre */
),
marginBottom
:
mm2inches
(
10
/* millimetre */
),
marginLeft
:
mm2inches
(
10
/* millimetre */
),
marginRight
:
mm2inches
(
10
/* millimetre */
),
pageRanges
:
""
,
headerTemplate
:
""
,
footerTemplate
:
""
,
preferCSSPageSize
:
false
}
/*
/*
Usage: Start google chromium with the following command line flags.
Usage: Start google chromium with the following command line flags.
--headless
--headless
...
@@ -24,7 +51,7 @@
...
@@ -24,7 +51,7 @@
{
{
var
page
=
await
browser
.
openNewPage
(
url
)
var
page
=
await
browser
.
openNewPage
(
url
)
await
browser
.
enablePage
(
page
)
await
browser
.
enablePage
(
page
)
var
pdf
=
browser
.
printToPdf
(
page
)
var
pdf
=
await
browser
.
printToPdf
(
page
,
optionsForPrintToPdf
)
await
browser
.
closePage
(
page
)
await
browser
.
closePage
(
page
)
return
pdf
return
pdf
}
}
...
@@ -39,7 +66,8 @@
...
@@ -39,7 +66,8 @@
var
url
=
handler
.
getNextRequest
()
var
url
=
handler
.
getNextRequest
()
var
pdf
=
await
html2pdf
(
url
)
var
pdf
=
await
html2pdf
(
url
)
handler
.
publishResults
(
pdf
)
var
name
=
(
new
URL
(
url
)).
hostname
handler
.
publishResults
(
name
,
pdf
)
setTimeout
(
mainloop
,
3
/* seconds */
*
1000
)
setTimeout
(
mainloop
,
3
/* seconds */
*
1000
)
}
}
mainloop
()
mainloop
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment