Advertisement
nubooya

Untitled

Jan 7th, 2022
52
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'
  2.  
  3. const gs = require('ghostscript4js')
  4.  
  5. const { PDFDocument, StandardFonts, rgb, degrees } = require('pdf-lib');
  6. const fs = require('fs');
  7.  
  8. run().catch(err => console.log(err));
  9.  
  10. async function run() {
  11. const content = await PDFDocument.load(fs.readFileSync('./go-thestdlib-sample.pdf'), { ignoreEncryption: true });
  12.  
  13. // Add a font to the doc
  14. const helveticaFont = await content.embedFont(StandardFonts.Helvetica);
  15.  
  16. let img = fs.readFileSync('./foto.png');
  17. img = await content.embedPng(img);
  18. const jpgDims = img.scale(0.2)
  19.  
  20.  
  21. // Begin Split File
  22. const doc = await PDFDocument.create();
  23. const viewerPrefs = content.catalog.getOrCreateViewerPreferences();
  24. viewerPrefs.setHideToolbar(true);
  25. viewerPrefs.setDisplayDocTitle(false);
  26. viewerPrefs.setHideMenubar(true);
  27.  
  28. const halaman = [];
  29. console.log(content.getPageCount(), 'Total Halaman');
  30. let removePage = Math.round(content.getPageCount() * .2);
  31. removePage = removePage < 10 ? 10 : removePage > 30 ? 30 : removePage;
  32.  
  33. console.log(removePage, 'Halaman');
  34.  
  35. const totalPages = content.getPageCount();
  36. for (let index = removePage; index < totalPages; index++) {
  37. halaman.push(index);
  38. content.removePage(removePage);
  39. }
  40. console.log(halaman);
  41. const newPage = await doc.copyPages(content, halaman);
  42.  
  43. newPage.forEach(element => {
  44. doc.addPage(element);
  45. });
  46. // End of Split file
  47.  
  48. // Draw a number at the bottom of each page.
  49. // Note that the bottom of the page is `y = 0`, not the top
  50. const pages = await content.getPages();
  51.  
  52. // const { width, height } = img.scale(1);
  53. for (const [i, page] of Object.entries(pages)) {
  54. if (i > 0) {
  55. page.drawText(
  56. // `Halaman: ${i} --- @Sinau Edukasi --- ${(new Date()).toLocaleString()}`,
  57. `FreeBook: @Moco - ${(new Date()).toLocaleString()}`,
  58. {
  59. x: 20,
  60. y: 30,
  61. size: 10,
  62. font: helveticaFont,
  63. color: rgb(0, 0, 0),
  64. rotate: degrees(90)
  65. }
  66. );
  67.  
  68. page.drawText(
  69. `FreeBook - Sample\n\n Print @${(new Date()).toLocaleString()}`,
  70. {
  71. x: (page.getWidth() / 2) - (page.getWidth() / 4),
  72. y: page.getHeight() / 4,
  73. size: 40,
  74. font: helveticaFont,
  75. color: rgb(0, 0, 0),
  76. rotate: degrees(45),
  77. opacity: .3
  78. }
  79. );
  80. }
  81.  
  82. // page.drawImage(img, {
  83. // // x: 25,
  84. // // y: 25,
  85. // x: Math.floor(Math.random() * (width - 100) + width / 2),
  86. // y: Math.floor(Math.random() * (height - 140) + height / 2),
  87. // width: jpgDims.width,
  88. // height: jpgDims.height,
  89. // rotate: degrees(30),
  90. // opacity: 0.3,
  91. // })
  92. }
  93.  
  94. // Write the PDF to a file
  95. fs.writeFileSync('./halaman.pdf', await content.save());
  96.  
  97. const inputFile = './halaman.pdf';
  98. const outputFilePath = Date.now() + "-output.pdf";
  99.  
  100. await gs.executeSync(
  101. `gs
  102. \ -q -dNOPAUSE -dBATCH -dSAFER
  103. -dPrinted=false
  104. -dDownsampleColorImages=true
  105. -dDownsampleGrayImages=true
  106. -dDownsampleMonoImages=true
  107. -r144
  108. \ -sDEVICE=pdfwrite
  109. \ -dCompatibilityLevel=1.4
  110. \ -dPDFSETTINGS=/ebook
  111. \ -dEmbedAllFonts=true
  112. \ -dSubsetFonts=true
  113. \ -dAutoRotatePages=/None
  114. \ -dColorImageDownsampleType=/Bicubic
  115. \ -dColorImageResolution=144
  116. \ -dGrayImageDownsampleType=/Bicubic
  117. \ -dGrayImageResolution=144
  118. \ -dMonoImageDownsampleType=/Subsample
  119. \ -dMonoImageResolution=144
  120. -dColorImageDownsampleThreshold=1.0
  121. -dGrayImageDownsampleThreshold=1.0
  122. -dMonoImageDownsampleThreshold=1.0
  123. \ -sOutputFile=${outputFilePath}
  124. \ ${inputFile}`
  125. );
  126.  
  127. console.log('Proses Selesai')
  128. }
  129.  
Advertisement
RAW Paste Data Copied
Advertisement