31app.route("/api/merge", mergeRoute);
32app.route("/api/split", splitRoute);
33app.route("/api/pdf-to-images", convertRoute);
34app.route("/api/images-to-pdf", convertRoute);
35app.route("/api/compress", compressRoute);
36app.route("/api/watermark", watermarkRoute);
pdfconvert.ts24 matches
5const app = new Hono();
67// PDF to Images conversion
8app.post("/", async (c) => {
9try {
11const operation = url.pathname.split('/').pop();
12
13if (operation === 'pdf-to-images') {
14return await convertPdfToImages(c);
15} else if (operation === 'images-to-pdf') {
16return await convertImagesToPdf(c);
17}
18
27});
2829async function convertPdfToImages(c: any) {
30const formData = await c.req.formData();
31const file = formData.get('file0') as File;
37}
3839// Note: PDF to image conversion requires canvas/image processing
40// This is a simplified implementation that would need additional libraries
41// like pdf2pic or similar for actual image conversion
42
43return c.json({
44success: false,
45error: "PDF to image conversion requires additional image processing libraries. This feature is not yet implemented in this demo."
46}, 501);
47}
4849async function convertImagesToPdf(c: any) {
50const formData = await c.req.formData();
51const files: File[] = [];
5960if (files.length === 0) {
61return c.json({ success: false, error: "Please upload at least one image file" }, 400);
62}
636566for (const file of files) {
67if (!file.type.startsWith('image/')) {
68return c.json({ success: false, error: `File ${file.name} is not an image` }, 400);
69}
7071const arrayBuffer = await file.arrayBuffer();
72let image;
7374try {
75if (file.type === 'image/jpeg' || file.type === 'image/jpg') {
76image = await pdf.embedJpg(arrayBuffer);
77} else if (file.type === 'image/png') {
78image = await pdf.embedPng(arrayBuffer);
79} else {
80return c.json({ success: false, error: `Unsupported image format: ${file.type}` }, 400);
81}
8283const page = pdf.addPage();
84const { width, height } = image.scale(1);
85
86// Scale image to fit page while maintaining aspect ratio
87const pageWidth = page.getWidth();
88const pageHeight = page.getHeight();
92const scaledHeight = height * scale;
93
94page.drawImage(image, {
95x: (pageWidth - scaledWidth) / 2,
96y: (pageHeight - scaledHeight) / 2,
99});
100} catch (error) {
101return c.json({ success: false, error: `Failed to process image ${file.name}` }, 400);
102}
103}
108headers: {
109'Content-Type': 'application/pdf',
110'Content-Disposition': 'attachment; filename="images-to-pdf.pdf"',
111'Content-Length': pdfBytes.length.toString(),
112},
pdfProcessingOptions.tsx1 match
37);
3839case 'pdf-to-images':
40return (
41<div className="space-y-4">
testPondiverseupdateTable1 match
9data TEXT,
10type TEXT,
11image TEXT,
12time DATETIME NOT NULL
13)`,
54},
55{
56id: 'pdf-to-images',
57name: 'PDF to Images',
58description: 'Convert PDF pages to PNG or JPG images',
59icon: '🖼️',
60acceptedFiles: ['.pdf']
61},
62{
63id: 'images-to-pdf',
64name: 'Images to PDF',
65description: 'Convert images to PDF format',
66icon: '📄',
67acceptedFiles: ['.jpg', '.jpeg', '.png', '.gif', '.bmp'],
78id: 'watermark',
79name: 'Add Watermark',
80description: 'Add text or image watermarks to PDF pages',
81icon: '💧',
82acceptedFiles: ['.pdf']
7- **Merge PDFs**: Combine multiple PDF files into one
8- **Split PDF**: Extract specific pages or split into separate files
9- **PDF to Images**: Convert PDF pages to PNG/JPG images
10- **Images to PDF**: Convert images to PDF format
11- **Compress PDF**: Reduce PDF file size
12- **Add Watermark**: Add text or image watermarks
13- **Extract Text**: Extract text content from PDFs
14- **Rotate Pages**: Rotate PDF pages
47481. Select the PDF operation you want to perform
492. Upload your PDF file(s) or images
503. Configure operation settings
514. Process and download the result
31- `RotateOptions`: Rotation angle and pages
32- `ProtectOptions`: Password and permissions
33- `ConversionOptions`: Image format and quality
3435### ProcessingResult
16- Merge PDFs
17- Split PDF
18- PDF to Images
19- Images to PDF
20- Compress PDF
21- PDF Info
34### File Upload
35- Drag and drop support
36- Multiple file selection for merge/images-to-pdf
37- File type validation
38- File size display
43- **Rotate**: Angle selection and page targeting
44- **Protect**: Password input
45- **PDF to Images**: Format and quality selection
4647### Processing
19- **Response**: PDF file download or JSON error
2021### POST /api/pdf/pdf-to-images
22Convert PDF pages to images (info only - actual conversion requires additional libraries).
23- **Body**: FormData with `file0` and `options`
24- **Options**: `{ format?: 'png'|'jpg', dpi?: number }`
25- **Response**: JSON with conversion info
2627### POST /api/pdf/images-to-pdf
28Convert images to a PDF document.
29- **Body**: FormData with multiple image files
30- **Response**: PDF file download or JSON error
31
138});
139140// PDF to Images (simplified - returns info about conversion)
141app.post("/pdf-to-images", async (c) => {
142try {
143const { files, options } = await parseFormData(c.req.raw);
150const info = await getPDFInfo(pdfBytes);
151
152// Note: Actual image conversion would require additional libraries like pdf2pic
153// For now, we return information about what would be converted
154return c.json({
165});
166167// Images to PDF
168app.post("/images-to-pdf", async (c) => {
169try {
170const { files } = await parseFormData(c.req.raw);
171
172if (files.length === 0) {
173return c.json({ success: false, message: "At least one image file is required" });
174}
175177178for (const file of files) {
179const imageBytes = new Uint8Array(await file.arrayBuffer());
180let image;
181182if (file.type === 'image/png') {
183image = await pdfDoc.embedPng(imageBytes);
184} else if (file.type === 'image/jpeg' || file.type === 'image/jpg') {
185image = await pdfDoc.embedJpg(imageBytes);
186} else {
187continue; // Skip unsupported formats
189190const page = pdfDoc.addPage();
191const { width, height } = image.scale(1);
192
193// Scale image to fit page
194const pageWidth = page.getWidth();
195const pageHeight = page.getHeight();
199const scaledHeight = height * scale;
200
201page.drawImage(image, {
202x: (pageWidth - scaledWidth) / 2,
203y: (pageHeight - scaledHeight) / 2,
212headers: {
213'Content-Type': 'application/pdf',
214'Content-Disposition': 'attachment; filename="images.pdf"'
215}
216});
218return c.json({
219success: false,
220message: error instanceof Error ? error.message : "Failed to create PDF from images"
221});
222}