pdfbooklet 1.0.5 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/pdfbooklet.js +20 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdfbooklet",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Create booklet from a pdf file suitable for compact printing",
5
5
  "main": "pdfbooklet.js",
6
6
  "bin": {
package/pdfbooklet.js CHANGED
@@ -69,13 +69,19 @@ async function createBooklet(inputPdfPath, outputPdfPath) {
69
69
  const totalPages = pdfDoc.getPageCount();
70
70
 
71
71
  // If no page numbers specified, use the whole range
72
- const pageNumbers = inputPageNumbers || Array.from({length: totalPages}, (_, i) => i);
72
+ const pageNumbers = inputPageNumbers || []
73
+ if (pageNumbers.length == 0) {
74
+ for (let i = 0; i < totalPages; i++) {
75
+ pageNumbers.push(i);
76
+ }
77
+ }
78
+
73
79
  // Align the numbers to a multiple of 4, use blank page for padding
74
80
  while (pageNumbers.length % 4 !== 0) {
75
81
  pageNumbers.push(-1);
76
82
  }
77
83
 
78
- console.log(`Total pages in the input PDF: ${totalPages}`);
84
+ console.log(`Total pages in the input PDF: ${totalPages}, selected pages (0-based) =`, pageNumbers);
79
85
 
80
86
  // Create a new PDF for the booklet
81
87
  const bookletPdf = await PDFDocument.create();
@@ -101,7 +107,10 @@ async function createBooklet(inputPdfPath, outputPdfPath) {
101
107
  }
102
108
 
103
109
  const drawPageInBox = async (newPage, box, originPageIndex, flip) => {
104
- if (originPageIndex < 0) return;
110
+ if (originPageIndex < 0) {
111
+ // Do not draw for page number -1
112
+ return;
113
+ }
105
114
  const a = await getEmbedPage(originPageIndex)
106
115
  if (a) {
107
116
  const {width, height} = a.size()
@@ -117,6 +126,8 @@ async function createBooklet(inputPdfPath, outputPdfPath) {
117
126
  xScale: scale,
118
127
  yScale: scale,
119
128
  })
129
+ } else {
130
+ console.error("getEmbedPage failed at page " + originPageIndnex);
120
131
  }
121
132
  }
122
133
 
@@ -221,8 +232,12 @@ if (!fs.existsSync(inputPdfPath)) {
221
232
  // Automatically generate the output filename by appending '-booklet' before '.pdf'
222
233
  outputPdfPath ||= inputPdfPath.replace(/\.pdf$/i, '-booklet.pdf');
223
234
 
235
+ if (outputPdfPath === inputPdfPath) {
236
+ err("Output file must not be same as input file");
237
+ }
238
+
224
239
  // Usage example
225
- createBooklet(inputPdfPath, outputPdfPath).catch(err => {
226
- console.error('Error creating booklet:', err);
240
+ createBooklet(inputPdfPath, outputPdfPath).catch(e => {
241
+ err(`Error creating booklet: ${e}`);
227
242
  });
228
243