Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict'
- const gs = require('ghostscript4js')
- const { PDFDocument, StandardFonts, rgb, degrees } = require('pdf-lib');
- const fs = require('fs');
- run().catch(err => console.log(err));
- async function run() {
- const content = await PDFDocument.load(fs.readFileSync('./go-thestdlib-sample.pdf'), { ignoreEncryption: true });
- // Add a font to the doc
- const helveticaFont = await content.embedFont(StandardFonts.Helvetica);
- let img = fs.readFileSync('./foto.png');
- img = await content.embedPng(img);
- const jpgDims = img.scale(0.2)
- // Begin Split File
- const doc = await PDFDocument.create();
- const viewerPrefs = content.catalog.getOrCreateViewerPreferences();
- viewerPrefs.setHideToolbar(true);
- viewerPrefs.setDisplayDocTitle(false);
- viewerPrefs.setHideMenubar(true);
- const halaman = [];
- console.log(content.getPageCount(), 'Total Halaman');
- let removePage = Math.round(content.getPageCount() * .2);
- removePage = removePage < 10 ? 10 : removePage > 30 ? 30 : removePage;
- console.log(removePage, 'Halaman');
- const totalPages = content.getPageCount();
- for (let index = removePage; index < totalPages; index++) {
- halaman.push(index);
- content.removePage(removePage);
- }
- console.log(halaman);
- const newPage = await doc.copyPages(content, halaman);
- newPage.forEach(element => {
- doc.addPage(element);
- });
- // End of Split file
- // Draw a number at the bottom of each page.
- // Note that the bottom of the page is `y = 0`, not the top
- const pages = await content.getPages();
- // const { width, height } = img.scale(1);
- for (const [i, page] of Object.entries(pages)) {
- if (i > 0) {
- page.drawText(
- // `Halaman: ${i} --- @Sinau Edukasi --- ${(new Date()).toLocaleString()}`,
- `FreeBook: @Moco - ${(new Date()).toLocaleString()}`,
- {
- x: 20,
- y: 30,
- size: 10,
- font: helveticaFont,
- color: rgb(0, 0, 0),
- rotate: degrees(90)
- }
- );
- page.drawText(
- `FreeBook - Sample\n\n Print @${(new Date()).toLocaleString()}`,
- {
- x: (page.getWidth() / 2) - (page.getWidth() / 4),
- y: page.getHeight() / 4,
- size: 40,
- font: helveticaFont,
- color: rgb(0, 0, 0),
- rotate: degrees(45),
- opacity: .3
- }
- );
- }
- // page.drawImage(img, {
- // // x: 25,
- // // y: 25,
- // x: Math.floor(Math.random() * (width - 100) + width / 2),
- // y: Math.floor(Math.random() * (height - 140) + height / 2),
- // width: jpgDims.width,
- // height: jpgDims.height,
- // rotate: degrees(30),
- // opacity: 0.3,
- // })
- }
- // Write the PDF to a file
- fs.writeFileSync('./halaman.pdf', await content.save());
- const inputFile = './halaman.pdf';
- const outputFilePath = Date.now() + "-output.pdf";
- await gs.executeSync(
- `gs
- \ -q -dNOPAUSE -dBATCH -dSAFER
- -dPrinted=false
- -dDownsampleColorImages=true
- -dDownsampleGrayImages=true
- -dDownsampleMonoImages=true
- -r144
- \ -sDEVICE=pdfwrite
- \ -dCompatibilityLevel=1.4
- \ -dPDFSETTINGS=/ebook
- \ -dEmbedAllFonts=true
- \ -dSubsetFonts=true
- \ -dAutoRotatePages=/None
- \ -dColorImageDownsampleType=/Bicubic
- \ -dColorImageResolution=144
- \ -dGrayImageDownsampleType=/Bicubic
- \ -dGrayImageResolution=144
- \ -dMonoImageDownsampleType=/Subsample
- \ -dMonoImageResolution=144
- -dColorImageDownsampleThreshold=1.0
- -dGrayImageDownsampleThreshold=1.0
- -dMonoImageDownsampleThreshold=1.0
- \ -sOutputFile=${outputFilePath}
- \ ${inputFile}`
- );
- console.log('Proses Selesai')
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement