
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
@itaylor/pdf2square
Advanced tools
CLI and Library: Convert PDF pages to exactly NxN images (letterboxed), using pdfjs + Sharp
Convert PDF pages to exactly square (NxN) images with letterboxing, plus extract text from each page. Available as both a CLI tool and a JavaScript library.
No system dependencies required - uses PDF.js for PDF processing and Sharp for image manipulation.
npm install pdf2square
Or for global CLI usage:
npm install -g pdf2square
# Convert PDF to 896x896 PNG images + text files
pdf2square input.pdf
# Specify output prefix
pdf2square input.pdf output/page
# Convert with custom options
pdf2square input.pdf --size 512 --dpi 300 --format jpg
| Option | Description | Default |
|---|---|---|
-n, --max-pages <int> | Maximum pages to convert | 10 |
-s, --size <int> | Target square size in pixels | 896 |
--dpi <int> | Render DPI (higher = crisper text) | 700 |
--first <int> | First page to convert (1-based) | 1 |
--format <fmt> | Output format: png or jpg | png |
--bg <color> | Background color (#RRGGBB[AA] or 'transparent') | #ffffffff |
--concurrency <int> | Max parallel processes | 4 |
--keep-intermediate | Keep intermediate renders | false |
# Convert first 5 pages to 512x512 JPEG with white background
pdf2square document.pdf --max-pages 5 --size 512 --format jpg --bg "#ffffff"
# Convert pages 3-7 with transparent background (PNG only)
pdf2square document.pdf --first 3 --max-pages 5 --bg transparent
# High DPI conversion for crisp text
pdf2square document.pdf --dpi 1000 --size 1024
# Process with higher concurrency
pdf2square document.pdf --concurrency 8
import { convert } from 'pdf2square';
// Convert a PDF file to square images with extracted text
const results = await convert('./path/to/your/document.pdf');
results.forEach((page) => {
console.log(`Page ${page.pageNumber}:`);
console.log(`- Image: ${page.base64EncodedImage.substring(0, 50)}...`);
console.log(`- Text: ${page.extractedText.substring(0, 100)}...`);
});
convert(pathToPdf, options?)Converts PDF pages to base64 encoded square images with extracted text.
Parameters:
pathToPdf (string): Path to the input PDF fileoptions (object, optional): Conversion optionsReturns: Promise<ConvertedPDFPage[]>
interface ConvertedPDFPage {
pageNumber: number; // Page number (1-based)
originalPath: string; // Path to the original PDF file
base64EncodedImage: string; // Base64 encoded image with data URL prefix
extractedText: string; // Extracted text from the page
}
interface ConvertOptions {
maxPages?: number; // Maximum pages to convert (default: 10)
size?: number; // Target square size in pixels (default: 896)
dpi?: number; // Render DPI (default: 700)
first?: number; // First page to convert (default: 1)
format?: 'png' | 'jpg'; // Output format (default: 'png')
bg?: string; // Background color (default: '#ffffffff')
concurrency?: number; // Max parallel processes (default: 4)
}
import { convert } from 'pdf2square';
const results = await convert('./path/to/document.pdf', {
maxPages: 5,
size: 512,
dpi: 300,
format: 'jpg',
bg: '#ffffff',
concurrency: 2,
});
import { convert } from 'pdf2square';
import fs from 'node:fs/promises';
const results = await convert('./path/to/document.pdf');
for (const page of results) {
// Extract base64 data (remove data URL prefix)
const base64Data = page.base64EncodedImage.replace(
/^data:image\/\w+;base64,/,
'',
);
const buffer = Buffer.from(base64Data, 'base64');
// Save image and text files
await fs.writeFile(`page-${page.pageNumber}.png`, buffer);
await fs.writeFile(`page-${page.pageNumber}.txt`, page.extractedText);
}
import { convert } from 'pdf2square';
// Express.js route handler example
export async function processPDF(req, res) {
try {
// req.file.path comes from multer file upload middleware
const results = await convert(req.file.path, {
maxPages: 10,
size: 896,
format: 'png',
});
res.json({
success: true,
totalPages: results.length,
pages: results.map((page) => ({
pageNumber: page.pageNumber,
image: page.base64EncodedImage,
text: page.extractedText,
textWordCount: page.extractedText.split(/\s+/).length,
})),
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message,
});
}
}
import { convert } from 'pdf2square';
async function processBatch(pdfPaths) {
const results = [];
for (const pdfPath of pdfPaths) {
console.log(`Processing ${pdfPath}...`);
try {
const pages = await convert(pdfPath, {
maxPages: 5,
concurrency: 2, // Lower concurrency when processing multiple files
});
results.push({
pdfPath,
success: true,
pageCount: pages.length,
data: pages,
});
} catch (error) {
console.error(`Failed to process ${pdfPath}:`, error.message);
results.push({
pdfPath,
success: false,
error: error.message,
});
}
}
return results;
}
// Example usage:
// const results = await processBatch([
// './documents/file1.pdf',
// './documents/file2.pdf',
// './documents/file3.pdf'
// ]);
When using the CLI, files are saved with the following naming convention:
input.pdf → input-001.png, input-001.txt
→ input-002.png, input-002.txt
→ ...
The library returns base64 encoded images with data URL prefixes:
{
pageNumber: 1,
originalPath: "/path/to/input.pdf",
base64EncodedImage: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...",
extractedText: "This is the text content from page 1..."
}
Supported background color formats:
#RRGGBB or #RRGGBBAAtransparent (PNG only)#ffffff, #ff0000aa, transparentNote: JPEG format cannot be transparent and will fallback to white background.
--concurrency based on your CPU cores and available
memory--first and --max-pages to process only the pages you
needCommon errors and solutions:
--first and --max-pages parametersThe library includes TypeScript declarations for better development experience:
import { convert, ConvertedPDFPage, ConvertOptions } from 'pdf2square';
const options: ConvertOptions = {
maxPages: 5,
size: 512,
format: 'png',
dpi: 300,
bg: '#ffffff',
};
const results: ConvertedPDFPage[] = await convert(
'./path/to/document.pdf',
options,
);
// Process results with full type safety
results.forEach((page: ConvertedPDFPage) => {
console.log(
`Page ${page.pageNumber}: ${page.extractedText.length} characters`,
);
});
MIT License - see LICENSE file for details.
FAQs
CLI and Library: Convert PDF pages to exactly NxN images (letterboxed), using pdfjs + Sharp
We found that @itaylor/pdf2square demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.