Convert PPTX presentations to PDF effortlessly with TypeScript.
Project details
The pptx-to-pdf library offers a pure TypeScript solution to convert PPTX files into PDF format without relying on LibreOffice or other native dependencies. With easy-to-use methods, it allows for seamless integration into applications, supporting custom slides, fonts, and more.
The pptx-to-pdf library is a pure TypeScript solution for converting PPTX files to PDF format without the need for any native dependencies or external applications like LibreOffice. This lightweight converter takes in a PPTX buffer and outputs a PDF buffer, making it simple to integrate into various environments such as server-side applications or serverless functions.
npm install pptx-to-pdf and start converting PPTX presentations with minimal setup.Integrate the conversion functionality seamlessly:
import { convert } from 'pptx-to-pdf';
import fs from 'fs';
const pptx = fs.readFileSync('presentation.pptx');
const pdf = await convert(pptx);
fs.writeFileSync('output.pdf', pdf);
The main function, convert, allows for various configurations:
convert(pptxBuffer: Buffer, options?): Promise<Buffer>
| Option | Type | Default | Description |
|---|---|---|---|
slides | number[] | all | Indices of slides to include (zero-based indexing) |
fontDirs | string[] | [] | Directories for additional font searching |
fontMap | `Record<string, string | Buffer>` | {} |
dpi | number | 96 | Sets the output resolution |
onWarning | (msg: string) | no-op | Callback for handling non-fatal warnings |
Convert only specific slides:
const pdf = await convert(pptx, {
slides: [0, 2, 4], // Include first, third, and fifth slides
});
Handle custom fonts by specifying font directories and mappings:
const pdf = await convert(pptx, {
fontDirs: ['/usr/share/fonts/truetype'],
fontMap: {
'Calibri': '/path/to/calibri.ttf',
'Custom Font': fs.readFileSync('/path/to/custom.ttf'),
},
});
This library can be utilized in various contexts:
app.post('/convert', async (req, res) => {
const pdf = await convert(req.body);
res.type('application/pdf').send(pdf);
});
export const handler = async (event) => {
const pptx = Buffer.from(event.body, 'base64');
const pdf = await convert(pptx);
return {
statusCode: 200,
headers: { 'Content-Type': 'application/pdf' },
body: pdf.toString('base64'),
isBase64Encoded: true,
};
};
The library effectively handles:
Currently, the library does not support:
The internal process of the library involves parsing the PPTX file (a packaged ZIP archive) and rendering it directly into a PDF format. It utilizes libraries like jszip for extraction and pdfkit for rendering, avoiding the overhead of intermediate HTML or browser-based processing.
For enhanced fidelity, include actual fonts in the conversion process using custom font directories or mappings. The library includes default metric-compatible font fallbacks for basic compatibility.
Comments
0Start the conversation
Share the first comment.