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
4a7c3d19
Commit
4a7c3d19
authored
Jun 13, 2022
by
lukas.niegsch
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored syntax
parent
30768ecb
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
85 additions
and
71 deletions
+85
-71
chromium.js
chromium.js
+67
-41
connection.js
connection.js
+0
-6
index.html
index.html
+18
-24
No files found.
chromium.js
View file @
4a7c3d19
class
HeadlessChromium
class
HeadlessChromium
{
{
constructor
(
websocket
Url
)
constructor
(
websocket
)
{
{
this
.
websocketUrl
=
websocketUrl
this
.
websocket
=
websocket
this
.
websocket
=
new
WebSocket
(
websocketUrl
)
}
}
isConnected
(
)
async
runCommand
(
command
,
params
=
{}
)
{
{
return
this
.
websocket
.
readyState
==
WebSocket
.
OPEN
return
new
Promise
((
resolve
,
reject
)
=>
{
var
server
=
new
WebSocket
(
this
.
websocket
)
server
.
onopen
=
()
=>
{
var
message
=
{
id
:
0
,
method
:
command
,
params
:
params
}
server
.
send
(
JSON
.
stringify
(
message
))
}
}
runCommand
(
method
,
params
=
{},
callback
)
server
.
onmessage
=
(
response
)
=>
{
server
.
close
()
var
data
=
JSON
.
parse
(
response
.
data
)
if
(
'
result
'
in
data
)
{
{
var
command
=
{
id
:
0
,
method
:
method
,
params
:
params
}
resolve
(
data
.
result
)
this
.
websocket
.
send
(
JSON
.
stringify
(
command
))
this
.
websocket
.
onmessage
=
(
response
)
=>
{
callback
(
JSON
.
parse
(
response
.
data
))
}
}
}
openNewPage
(
url
,
callback
)
if
(
'
error
'
in
data
)
{
{
this
.
runCommand
(
"
Target.createTarget
"
,
{
url
:
url
},
(
response
)
=>
{
reject
(
data
.
error
)
callback
(
response
.
result
.
targetId
)
}
reject
(
data
)
}
server
.
onerror
=
(
error
)
=>
{
server
.
close
()
reject
(
error
)
}
})
})
}
}
closePage
(
page
,
callback
)
async
getVersion
(
)
{
{
this
.
runCommand
(
"
Target.closeTarget
"
,
{
targetId
:
page
},
(
response
)
=>
{
return
await
this
.
runCommand
(
"
Browser.getVersion
"
)
callback
()
})
}
}
focusPage
(
page
,
callback
)
async
enableDOM
(
)
{
{
this
.
runCommand
(
"
Target.activateTarget
"
,
{
targetId
:
page
},
(
response
)
=>
{
return
await
this
.
runCommand
(
"
DOM.enable
"
)
this
.
runCommand
(
"
Target.attachToTarget
"
,
{
targetId
:
page
},
(
response
)
=>
{
callback
()
})
})
}
}
// todo: does not work yet
async
enableNetwork
()
printToPdf
(
callback
)
{
{
this
.
runCommand
(
"
Page.enable
"
,
{},
(
ignored
)
=>
{
return
await
this
.
runCommand
(
"
Network.enable
"
)
console
.
log
(
"
Page.enable:
"
+
JSON
.
stringify
(
ignored
))
this
.
runCommand
(
"
Page.printToPDF
"
,
{},
(
response
)
=>
{
console
.
log
(
"
Page.printToPDF:
"
+
JSON
.
stringify
(
response
))
})
})
}
}
}
async
enablePage
()
{
return
await
this
.
runCommand
(
"
Page.enable
"
)
}
async
openNewPage
(
url
)
{
var
response
=
await
this
.
runCommand
(
"
Target.createTarget
"
,
{
url
:
url
})
return
response
.
targetId
}
async
closePage
(
page
)
{
var
response
=
await
this
.
runCommand
(
"
Target.closeTarget
"
,
{
targetId
:
page
})
return
response
.
success
}
async
focusPage
(
page
)
{
await
this
.
runCommand
(
"
Target.activateTarget
"
,
{
targetId
:
page
})
}
async
printToPdf
()
{
var
response
=
await
this
.
runCommand
(
"
Page.printToPDF
"
)
return
response
}
}
connection.js
View file @
4a7c3d19
function
test
()
{
return
"
test
"
}
index.html
View file @
4a7c3d19
...
@@ -10,34 +10,28 @@
...
@@ -10,34 +10,28 @@
var
searchParams
=
(
new
URL
(
window
.
location
.
href
)).
searchParams
var
searchParams
=
(
new
URL
(
window
.
location
.
href
)).
searchParams
var
websocketUrl
=
searchParams
.
get
(
"
websocket
"
)
var
websocketUrl
=
searchParams
.
get
(
"
websocket
"
)
var
browser
=
new
HeadlessChromium
(
websocketUrl
)
var
browser
=
new
HeadlessChromium
(
websocketUrl
)
var
urls
=
[
"
https://www.example.com
"
]
function
html2pdf
(
url
,
callback
)
async
function
html2pdf
(
url
)
{
{
if
(
!
browser
.
isConnected
()
)
var
page
=
await
browser
.
openNewPage
(
url
)
{
await
browser
.
focusPage
(
page
)
console
.
log
(
"
Browser is not connected!
"
)
var
pdf
=
browser
.
printToPdf
(
)
callback
(
null
)
await
browser
.
closePage
(
page
)
return
return
pdf
}
}
browser
.
openNewPage
(
url
,
(
page
)
=>
{
async
function
mainloop
()
browser
.
focusPage
(
page
,
()
=>
{
{
browser
.
printToPdf
(()
=>
{
if
(
urls
.
length
==
0
)
browser
.
closePage
(
page
,
()
=>
{
{
console
.
log
(
"
close
"
)
setTimeout
(
mainloop
,
30
/* seconds */
*
1000
)
})
return
})
})
})
}
}
function
mainloop
()
var
pdf
=
await
html2pdf
(
urls
.
pop
())
{
html2pdf
(
"
http://www.example.com
"
,
(
pdf
)
=>
{
console
.
log
(
pdf
)
console
.
log
(
pdf
)
setTimeout
(
mainloop
,
3
/* seconds */
*
1000
)
setTimeout
(
mainloop
,
5
/* seconds */
*
1000
)
})
}
}
mainloop
()
mainloop
()
</script>
</script>
...
...
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