dichovsky / pdf-to-png-converter

Library Convert PDF to PNG
MIT License
121 stars 26 forks source link

Bug: Fix numbering system #52

Open Zirafnik opened 1 month ago

Zirafnik commented 1 month ago

Current numbering system, where just the actual number is appended to the end of the file name is very bad. Machines usually sort the filenames alphabetically which causes the images to be out of order when processing them programmatically!

This is default behavior for functions such as ls on UNIX systems or fsPromises.readdir(dirName) in Node.js, and produces the following output:

image_page_1.png
image_page_10.png
image_page_101.png
image_page_2.png
image_page_27.png
...

To fix this issue you have to zero pad the numbers: e.g. '1' -> '001', '27 -> '027',
'103' -> '103', ...

To get around this issue, I currently have to rename all the images afterwards, by first removing the .png extension, then splitting them by '_', then padding the numbers, and finally joining them back together.

This is the function I use to add zeros to the beginning of numbers:

/**
 * Pads the beginning of a number, with the appropriate amount of zeros to equal the final desired length.
 * @param { number } num the number we wish to pad
 * @param { number } places the total padded length of the string
 * @returns { string } zero padded string e.g. (12, 3) => '012'
 */
export const zeroPad = (num, places) => String(num).padStart(places, '0');

Padding by 3 places, allows for up to (& including) 999 ordered images.

Perhaps an additional option could be exposed, to choose the padding length (with default: 3).