Spaces:
Sleeping
Sleeping
Commit ·
b541c6e
1
Parent(s): bec2b0a
fix jsd
Browse files- README.md +10 -2
- app.py +1 -2
- puppeteer_pdf.js +15 -19
README.md
CHANGED
|
@@ -16,8 +16,8 @@ A powerful API to convert HTML documents to PDF with support for page breaks, im
|
|
| 16 |
## Features
|
| 17 |
|
| 18 |
- Convert HTML files to PDF
|
| 19 |
-
- Support for multiple aspect ratios (16:9, 1:1, 9:16)
|
| 20 |
-
- Automatic aspect ratio detection
|
| 21 |
- Image embedding support
|
| 22 |
- Page break support
|
| 23 |
- Base64 PDF output option
|
|
@@ -36,6 +36,9 @@ This guide covers the **optimized HTML to PDF conversion system** based on 2024-
|
|
| 36 |
### Example 1: Auto-Detection (Recommended)
|
| 37 |
```bash
|
| 38 |
curl -X POST https://abdallalswaiti-htmlpdfs.hf.space/convert \
|
|
|
|
|
|
|
|
|
|
| 39 |
-o output.pdf
|
| 40 |
```
|
| 41 |
|
|
@@ -212,6 +215,11 @@ img, figure {
|
|
| 212 |
...
|
| 213 |
```
|
| 214 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
---
|
| 216 |
|
| 217 |
## 🎯 Mode Selection Guide
|
|
|
|
| 16 |
## Features
|
| 17 |
|
| 18 |
- Convert HTML files to PDF
|
| 19 |
+
- Support for multiple aspect ratios (16:9, 1:1, 9:16, auto)
|
| 20 |
+
- Automatic aspect ratio detection from content
|
| 21 |
- Image embedding support
|
| 22 |
- Page break support
|
| 23 |
- Base64 PDF output option
|
|
|
|
| 36 |
### Example 1: Auto-Detection (Recommended)
|
| 37 |
```bash
|
| 38 |
curl -X POST https://abdallalswaiti-htmlpdfs.hf.space/convert \
|
| 39 |
+
-F "html_file=@document.html" \
|
| 40 |
+
-F "aspect_ratio=auto" \
|
| 41 |
+
-F "mode=single" \
|
| 42 |
-o output.pdf
|
| 43 |
```
|
| 44 |
|
|
|
|
| 215 |
...
|
| 216 |
```
|
| 217 |
|
| 218 |
+
### **Auto - Dynamic**
|
| 219 |
+
- **Best for**: Infographics, posters, or any content where the exact dimensions should be preserved.
|
| 220 |
+
- **Format**: The PDF will have the same dimensions as the HTML content.
|
| 221 |
+
- **Use case**: When you want the PDF to be a perfect snapshot of the HTML content.
|
| 222 |
+
|
| 223 |
---
|
| 224 |
|
| 225 |
## 🎯 Mode Selection Guide
|
app.py
CHANGED
|
@@ -517,8 +517,7 @@ async def convert(
|
|
| 517 |
aspect_ratio = aspect_ratio or "9:16"
|
| 518 |
mode = mode or "auto"
|
| 519 |
|
| 520 |
-
|
| 521 |
-
if aspect_ratio not in ["16:9", "1:1", "9:16"]:
|
| 522 |
raise HTTPException(400, "Invalid aspect ratio. Must be 16:9, 1:1, or 9:16")
|
| 523 |
|
| 524 |
if mode not in ["auto", "single", "multi"]:
|
|
|
|
| 517 |
aspect_ratio = aspect_ratio or "9:16"
|
| 518 |
mode = mode or "auto"
|
| 519 |
|
| 520 |
+
if aspect_ratio not in ["16:9", "1:1", "9:16", "auto"]:
|
|
|
|
| 521 |
raise HTTPException(400, "Invalid aspect ratio. Must be 16:9, 1:1, or 9:16")
|
| 522 |
|
| 523 |
if mode not in ["auto", "single", "multi"]:
|
puppeteer_pdf.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
const puppeteer = require('puppeteer');
|
| 3 |
const fs = require('fs');
|
| 4 |
|
| 5 |
-
const [htmlFile, aspectRatio = '
|
| 6 |
|
| 7 |
if (!htmlFile) {
|
| 8 |
console.error('Usage: node puppeteer_pdf.js <html_file> [aspect_ratio] [mode]');
|
|
@@ -98,25 +98,21 @@ const configs = {
|
|
| 98 |
if (pdfMode === 'single') {
|
| 99 |
// SINGLE PAGE MODE - Perfect for infographics, social media posts
|
| 100 |
console.log('Generating single-page PDF...');
|
| 101 |
-
|
| 102 |
-
const dimensions = await page.evaluate(() => ({
|
| 103 |
-
width: Math.max(
|
| 104 |
-
document.documentElement.scrollWidth,
|
| 105 |
-
document.body.scrollWidth
|
| 106 |
-
),
|
| 107 |
-
height: Math.max(
|
| 108 |
-
document.documentElement.scrollHeight,
|
| 109 |
-
document.body.scrollHeight
|
| 110 |
-
)
|
| 111 |
-
}));
|
| 112 |
-
|
| 113 |
-
console.log(` Content dimensions: ${dimensions.width}x${dimensions.height}px`);
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
await page.pdf({
|
| 122 |
path: pdfPath,
|
|
|
|
| 2 |
const puppeteer = require('puppeteer');
|
| 3 |
const fs = require('fs');
|
| 4 |
|
| 5 |
+
const [htmlFile, aspectRatio = 'auto', mode = 'auto'] = process.argv.slice(2);
|
| 6 |
|
| 7 |
if (!htmlFile) {
|
| 8 |
console.error('Usage: node puppeteer_pdf.js <html_file> [aspect_ratio] [mode]');
|
|
|
|
| 98 |
if (pdfMode === 'single') {
|
| 99 |
// SINGLE PAGE MODE - Perfect for infographics, social media posts
|
| 100 |
console.log('Generating single-page PDF...');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
let singleConfig;
|
| 103 |
+
if (aspectRatio === 'auto') {
|
| 104 |
+
const dimensions = await page.evaluate(() => ({
|
| 105 |
+
width: document.documentElement.scrollWidth,
|
| 106 |
+
height: document.documentElement.scrollHeight
|
| 107 |
+
}));
|
| 108 |
+
console.log(` Auto-detected dimensions: ${dimensions.width}x${dimensions.height}px`);
|
| 109 |
+
singleConfig = {
|
| 110 |
+
width: dimensions.width + 'px',
|
| 111 |
+
height: dimensions.height + 'px'
|
| 112 |
+
};
|
| 113 |
+
} else {
|
| 114 |
+
singleConfig = config.single;
|
| 115 |
+
}
|
| 116 |
|
| 117 |
await page.pdf({
|
| 118 |
path: pdfPath,
|