const { PDFDocument, StandardFonts, PDFFont, degrees, rgb, PDFNumber, PDFPage } = require('pdf-lib');
const fs = require("fs");
const rbgWrapper = (red, green, blue) => rgb(red / (255 / 100) / 100, green / (255 / 100) / 100, blue / (255 / 100) / 100);
// CONSTANTS
const STAMP_COLOR = [85, 0, 171];
// Method for create PDF with stamp
const createStampDoc = async () => {
const newPDF = await PDFDocument.create();
const width = 250;
// Embed the Helvetica font
const TimesRoman = await newPDF.embedFont(StandardFonts.TimesRoman)
// Creating page for stamp
const page = newPDF.addPage([width, 100]);
const drawStamp = (page, x, y) => {
let curY = y;
curY += 10;
page.moveTo(x, curY);
page.drawText("TEXT ONE", {
font: TimesRoman,
size: 10,
color: rbgWrapper(...STAMP_COLOR)
});
curY += 10;
page.moveTo(x, curY);
page.drawText("TEXT TWO", {
font: TimesRoman,
size: 10,
color: rbgWrapper(...STAMP_COLOR)
});
// Border of stamp
const height = curY - y + 10;
page.drawRectangle({
x: x,
y: y,
width: width,
height: height,
borderColor: rbgWrapper(...STAMP_COLOR),
borderWidth: 1,
opacity: 0.3,
color: rbgWrapper(255, 255, 255)
});
page.setHeight(height);
return { width, height };
};
drawStamp(page, 0, 0);
return newPDF;
};
async function main() {
// Creating new main PDF file
const mainPDF = await PDFDocument.create();
mainPDF.addPage([350, 400]);
mainPDF.addPage([350, 400]);
const stampPDF = await createStampDoc();
const [copiedStampPage] = await mainPDF.copyPages(stampPDF, [0]);
const embeddedPage = await mainPDF.embedPage(copiedStampPage);
// In each page embedding stamp page
const pages = mainPDF.getPages();
for (const page of pages) {
await page.drawPage(embeddedPage, { x: 100, y: 100, rotate: degrees(20), opacity: 1 });
}
const binaryFileWithStamp = await mainPDF.save();
fs.writeFileSync("stamp.pdf", Buffer.from(binaryFileWithStamp));
}
main();
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example</title>
<script
src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous"></script>
</head>
<body>
<h1 class="header"><a id="_Toc446404887"></a><a id="_Toc446498856"></a><a id="_Toc473706079"></a>Тест 1</h1>
<h2 class="date"><a id="_Toc446411230"></a><a id="_Toc446404888"></a><a id="_Toc446318335"></a><a id="_Toc446498857"></a><a id="_Toc473706080"></a>10.10.10</h2>
<p>P1</p>
<p>tags1</p>
<p>here1</p>
<a class="source"><a href="test1.html">test1.html</a></a>
<h1 class="header"><a id="_Toc446404887"></a><a id="_Toc446498856"></a><a id="_Toc473706079"></a>Тест 2</h1>
<h2 class="date"><a id="_Toc446411230"></a><a id="_Toc446404888"></a><a id="_Toc446318335"></a><a id="_Toc446498857"></a><a id="_Toc473706080"></a>11.11.11</h2>
<p>P2</p>
<p>tags2</p>
<p>here2</p>
<a class="source"><a href="test2.html">test2.html</a></a>
<script>
'use strict';
var article_selector = $('.header');
var arr = [];
var count = 0;
for (var item of article_selector) {
var next_selector = $(item).next();
var row = {};
row.title = $(item).text();
if (next_selector.attr('class') == 'date') {
row.date = next_selector.text();
}
var href = '';
var loop_selector = next_selector.next();
var i = 0;
row.body = '';
while (i != 1) {
if (loop_selector.attr('class') == undefined) {
row.body += loop_selector.text()+' ';
loop_selector = loop_selector.next();
} else {
if (loop_selector.attr('class') == 'source') {
var elem = loop_selector;
href = elem[0]['nextSibling']['href'];
}
i = 1;
break;
}
}
row.href = href;
arr.push(row);
count++;
}
console.log(arr);
</script>
</body>
</html>