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
Hide 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
{
constructor
(
websocket
Url
)
constructor
(
websocket
)
{
this
.
websocketUrl
=
websocketUrl
this
.
websocket
=
new
WebSocket
(
websocketUrl
)
this
.
websocket
=
websocket
}
async
runCommand
(
command
,
params
=
{})
{
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
))
}
server
.
onmessage
=
(
response
)
=>
{
server
.
close
()
var
data
=
JSON
.
parse
(
response
.
data
)
if
(
'
result
'
in
data
)
{
resolve
(
data
.
result
)
}
isConnected
()
if
(
'
error
'
in
data
)
{
reject
(
data
.
error
)
}
reject
(
data
)
}
server
.
onerror
=
(
error
)
=>
{
server
.
close
()
reject
(
error
)
}
})
}
async
getVersion
()
{
return
this
.
websocket
.
readyState
==
WebSocket
.
OPEN
return
await
this
.
runCommand
(
"
Browser.getVersion
"
)
}
runCommand
(
method
,
params
=
{},
callback
)
async
enableDOM
(
)
{
var
command
=
{
id
:
0
,
method
:
method
,
params
:
params
}
this
.
websocket
.
send
(
JSON
.
stringify
(
command
))
this
.
websocket
.
onmessage
=
(
response
)
=>
{
callback
(
JSON
.
parse
(
response
.
data
))
}
return
await
this
.
runCommand
(
"
DOM.enable
"
)
}
openNewPage
(
url
,
callback
)
async
enableNetwork
(
)
{
this
.
runCommand
(
"
Target.createTarget
"
,
{
url
:
url
},
(
response
)
=>
{
callback
(
response
.
result
.
targetId
)
})
return
await
this
.
runCommand
(
"
Network.enable
"
)
}
closePage
(
page
,
callback
)
async
enablePage
(
)
{
this
.
runCommand
(
"
Target.closeTarget
"
,
{
targetId
:
page
},
(
response
)
=>
{
callback
()
})
return
await
this
.
runCommand
(
"
Page.enable
"
)
}
focusPage
(
page
,
callback
)
async
openNewPage
(
url
)
{
this
.
runCommand
(
"
Target.activateTarget
"
,
{
targetId
:
page
},
(
response
)
=>
{
this
.
runCommand
(
"
Target.attachToTarget
"
,
{
targetId
:
page
},
(
response
)
=>
{
callback
()
})
})
var
response
=
await
this
.
runCommand
(
"
Target.createTarget
"
,
{
url
:
url
})
return
response
.
targetId
}
// todo: does not work yet
printToPdf
(
callback
)
{
this
.
runCommand
(
"
Page.enable
"
,
{},
(
ignored
)
=>
{
console
.
log
(
"
Page.enable:
"
+
JSON
.
stringify
(
ignored
))
this
.
runCommand
(
"
Page.printToPDF
"
,
{},
(
response
)
=>
{
console
.
log
(
"
Page.printToPDF:
"
+
JSON
.
stringify
(
response
)
)
})
})
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 @@
var
searchParams
=
(
new
URL
(
window
.
location
.
href
)).
searchParams
var
websocketUrl
=
searchParams
.
get
(
"
websocket
"
)
var
browser
=
new
HeadlessChromium
(
websocketUrl
)
function
html2pdf
(
url
,
callback
)
var
urls
=
[
"
https://www.example.com
"
]
async
function
html2pdf
(
url
)
{
if
(
!
browser
.
isConnected
())
{
console
.
log
(
"
Browser is not connected!
"
)
callback
(
null
)
return
}
browser
.
openNewPage
(
url
,
(
page
)
=>
{
browser
.
focusPage
(
page
,
()
=>
{
browser
.
printToPdf
(()
=>
{
browser
.
closePage
(
page
,
()
=>
{
console
.
log
(
"
close
"
)
})
})
})
})
var
page
=
await
browser
.
openNewPage
(
url
)
await
browser
.
focusPage
(
page
)
var
pdf
=
browser
.
printToPdf
()
await
browser
.
closePage
(
page
)
return
pdf
}
function
mainloop
()
async
function
mainloop
()
{
html2pdf
(
"
http://www.example.com
"
,
(
pdf
)
=>
{
console
.
log
(
pdf
)
setTimeout
(
mainloop
,
5
/* seconds */
*
1000
)
})
if
(
urls
.
length
==
0
)
{
setTimeout
(
mainloop
,
30
/* seconds */
*
1000
)
return
}
var
pdf
=
await
html2pdf
(
urls
.
pop
())
console
.
log
(
pdf
)
setTimeout
(
mainloop
,
3
/* seconds */
*
1000
)
}
mainloop
()
</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