DEPRECATED
webshot
Быстрый, а главное эффективный, способ "сфотографировать" веб-страницу.
Ключевые моменты
- Источником может быть html-строка, url (включая
file:)
- Результатом может быть бинарный файл, бинарный буфер; бинарный поток записи может быть приемником
- Поддерживаемые форматы результата:
pdf, png, jpeg
Под капотом electron
Установка
npm i @tsofist/webshot --save --production
Использование
import { startupShooter } "webshot";
const myShooter = startupShooter();
function shotPageByURLToBinaryBuffer(url: string, format: ShotFormat): Promise<Buffer> {
return myShooter
.then((shooter) => shooter.shotURL(format, url));
}
function shotPageByURLToFile(url: string, format: ShotFormat, filename: string): Promise<string> {
return myShooter
.then((shooter) => shooter.shotURL(format, url, filename));
}
function shotPageByURLToStream(url: string, format: ShotFormat, destination: NodeJS.WritableStream): Promise<NodeJS.WritableStream> {
return myShooter
.then((shooter) => shooter.shotURL(format, url, destination));
}
И это не все! Можно проделать тоже самое но с html!
import { startupShooter } "webshot";
const myShooter = startupShooter();
function shotHTMLToBinaryBuffer(html: string, format: ShotFormat): Promise<Buffer> {
return myShooter
.then((shooter) => shooter.shotHTML(format, url));
}
function shotHTMLToFile(html: string, format: ShotFormat, filename: string): Promise<string> {
return myShooter
.then((shooter) => shooter.shotHTML(format, url, filename));
}
function shotHTMLToStream(html: string, format: ShotFormat, destination: NodeJS.WritableStream): Promise<NodeJS.WritableStream> {
return myShooter
.then((shooter) => shooter.shotHTML(format, url, destination));
}
Как только вам надоест потребность в конверторе иссякнет, его можно остановить, либо уничтожить вовсе.
myShooter.shutdown(() => console.log("Shooter is shutted down"));
myShooter.halt();
Поддерживаемые форматы
Формат можно описывать следующим образом:
{
type: "pdf",
marginsType?: 0
|1
|2;
pageSize?: "Legal"
|"Letter"
|"Tabloid"
|"A3"|"A4"|"A5"
|{ height: number; width: number; };
printBackground?: boolean;
landscape?: boolean;
}
{
type: "png",
scaleFactor?: number;
size?: "auto"|{ height: number; width: number; };
}
{
type: "jpeg",
quality: number;
size?: "auto"|{ height: number; width: number; };
}
TODO
- cli (?)
- Тесты, много тестов!
- Очереди, пулы страниц, пулы electron-экземпляров
- Больше документации (?)