Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
pagedjs-cli
Manage
Activity
Members
Labels
Plan
Issues
62
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pagedjs
pagedjs-cli
Merge requests
!1
Add Printer class
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Add Printer class
printer
into
master
Overview
0
Commits
1
Pipelines
1
Changes
6
Merged
Boris Budini
requested to merge
printer
into
master
6 years ago
Overview
0
Commits
1
Pipelines
1
Changes
6
Expand
0
0
Merge request reports
Compare
master
master (base)
and
latest version
latest version
6b1480a3
1 commit,
6 years ago
6 files
+
2805
−
218
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
6
Search (e.g. *.vue) (Ctrl+P)
bin/paged
+
25
−
67
Options
#!/usr/bin/env node
const
Paged
=
require
(
'
pagedjs
'
);
const
puppeteer
=
require
(
'
puppeteer
'
);
const
program
=
require
(
'
commander
'
);
//
const
temp
= require("
temp").track(
);
const
Printer
=
require
(
"
../
"
);
const
path
=
require
(
'
path
'
);
const
fs
=
require
(
'
fs
'
);
const
{
promisify
}
=
require
(
'
util
'
);
const
writeFileAsync
=
promisify
(
fs
.
writeFile
);
const
replaceExt
=
require
(
'
replace-ext
'
);
const
express
=
require
(
'
express
'
);
const
app
=
express
();
const
PORT
=
9999
;
program
.
version
(
require
(
'
../package.json
'
).
version
)
.
arguments
(
'
[inputPath]
'
)
@@ -28,6 +23,7 @@ program
// .option('-ho, --hypher_only [str]', 'Only hyphenate passed elements selector, such as ".hyphenate, aside"')
// .option('-e, --encoding [type]', 'Set the encoding of the input html, defaults to "utf-8"')
.
option
(
'
-t, --timeout [ms]
'
,
'
Set a max timeout of [ms]
'
)
.
option
(
'
-x, --html
'
,
'
output html file
'
)
.
parse
(
process
.
argv
);
@@ -95,64 +91,26 @@ if (program.hyphenate) {
(
async
()
=>
{
const
browser
=
await
puppeteer
.
launch
({
headless
:
headless
// args: ['--no-sandbox', '--allow-file-access-from-files', '--enable-local-file-accesses']
});
const
page
=
await
browser
.
newPage
();
let
relativePath
=
path
.
resolve
(
dir
,
input
);
let
dirname
=
path
.
dirname
(
relativePath
);
let
basename
=
path
.
basename
(
relativePath
);
app
.
use
(
"
/print
"
,
express
.
static
(
dirname
))
let
scriptPath
=
path
.
resolve
(
dir
,
"
./node_modules/pagedjs/dist/
"
);
app
.
use
(
"
/polyfill
"
,
express
.
static
(
scriptPath
))
let
server
=
app
.
listen
(
PORT
);
await
page
.
goto
(
`http://localhost:
${
PORT
}
/print/
${
basename
}
?preview=true`
);
await
page
.
exposeFunction
(
'
PuppeteerLogger
'
,
(
msg
,
counter
)
=>
{
console
.
log
(
msg
,
counter
);
});
await
page
.
exposeFunction
(
'
onPagesRendered
'
,
async
(
msg
,
width
,
height
,
orientation
)
=>
{
console
.
log
(
msg
,
width
,
height
,
orientation
);
if
(
headless
)
{
let
pages
=
await
page
.
waitForSelector
(
"
.pagedjs_pages
"
);
let
pdf
=
await
page
.
pdf
({
path
:
output
,
printBackground
:
true
,
displayHeaderFooter
:
false
,
width
:
width
,
height
:
height
,
orientation
:
orientation
,
margin
:
{
top
:
0
,
right
:
0
,
bottom
:
0
,
left
:
0
,
}
}).
catch
((
e
)
=>
{
console
.
error
(
e
);
});
console
.
log
(
"
Saved to
"
,
output
);
server
.
close
();
await
browser
.
close
();
}
});
await
page
.
addScriptTag
({
url
:
`http://localhost:
${
PORT
}
/polyfill/paged.polyfill.js`
});
let
printer
=
new
Printer
(
headless
);
let
file
;
if
(
headless
)
{
let
options
=
{};
if
(
program
.
html
)
{
file
=
await
printer
.
html
(
input
,
options
);
output
=
replaceExt
(
output
,
'
.html
'
);
}
else
{
file
=
await
printer
.
pdf
(
input
,
options
);
}
}
else
{
printer
.
preview
(
input
);
}
if
(
file
)
{
fs
.
writeFile
(
output
,
file
,
(
err
)
=>
{
if
(
err
)
throw
err
;
console
.
log
(
'
Saved to
'
,
output
);
process
.
exit
(
0
);
});
}
})();