Skip to content

Commit c1dd760

Browse files
add(tests): PDF dimension demo script to generate examples with custom size rendering
1 parent 32d358f commit c1dd760

2 files changed

Lines changed: 239 additions & 1 deletion

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
coverage
2+
coverage
3+
pdf-demo-examples

pdf.demo.examples.sh

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
#!/bin/bash
2+
3+
# Test custom PDF dimensions against localhost:3000
4+
# Tests the fix for: Puppeteer ignores width/height when format is set
5+
6+
BASE_URL="http://localhost:3000/render"
7+
OUTPUT_DIR="pdf-demo-examples"
8+
9+
mkdir -p "$OUTPUT_DIR"
10+
11+
echo "=========================================="
12+
echo "PDF Custom Dimensions Test Suite"
13+
echo "=========================================="
14+
echo ""
15+
16+
# Test 1: Custom dimensions in mm
17+
echo "Test 1: Custom dimensions in mm (210mm x 400mm)"
18+
curl -s -X POST "$BASE_URL" \
19+
-H "Content-Type: application/json" \
20+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"210mm","height":"400mm","margin":{"top":"10mm","bottom":"10mm","left":"10mm","right":"10mm"},"printBackground":true}}' \
21+
--output "$OUTPUT_DIR/01-custom-mm.pdf"
22+
echo " -> $OUTPUT_DIR/01-custom-mm.pdf"
23+
24+
# Test 2: Custom dimensions in inches
25+
echo "Test 2: US Letter size in inches (8.5in x 11in)"
26+
curl -s -X POST "$BASE_URL" \
27+
-H "Content-Type: application/json" \
28+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"8.5in","height":"11in","margin":{"top":"0.5in","bottom":"0.5in","left":"0.5in","right":"0.5in"}}}' \
29+
--output "$OUTPUT_DIR/02-letter-inches.pdf"
30+
echo " -> $OUTPUT_DIR/02-letter-inches.pdf"
31+
32+
# Test 3: Custom dimensions in cm
33+
echo "Test 3: Custom dimensions in cm (15cm x 25cm)"
34+
curl -s -X POST "$BASE_URL" \
35+
-H "Content-Type: application/json" \
36+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"15cm","height":"25cm","margin":{"top":"1cm","bottom":"1cm","left":"1cm","right":"1cm"}}}' \
37+
--output "$OUTPUT_DIR/03-custom-cm.pdf"
38+
echo " -> $OUTPUT_DIR/03-custom-cm.pdf"
39+
40+
# Test 4: Custom dimensions in px
41+
echo "Test 4: Custom dimensions in px (600px x 800px)"
42+
curl -s -X POST "$BASE_URL" \
43+
-H "Content-Type: application/json" \
44+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"600px","height":"800px","margin":{"top":"0","bottom":"0","left":"0","right":"0"}}}' \
45+
--output "$OUTPUT_DIR/04-custom-px.pdf"
46+
echo " -> $OUTPUT_DIR/04-custom-px.pdf"
47+
48+
# Test 5: Square PDF
49+
echo "Test 5: Square PDF (200mm x 200mm)"
50+
curl -s -X POST "$BASE_URL" \
51+
-H "Content-Type: application/json" \
52+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"200mm","height":"200mm","margin":{"top":"0","bottom":"0","left":"0","right":"0"}}}' \
53+
--output "$OUTPUT_DIR/05-square.pdf"
54+
echo " -> $OUTPUT_DIR/05-square.pdf"
55+
56+
# Test 6: Tall narrow PDF
57+
echo "Test 6: Tall narrow PDF (100mm x 400mm)"
58+
curl -s -X POST "$BASE_URL" \
59+
-H "Content-Type: application/json" \
60+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"100mm","height":"400mm","margin":{"top":"0","bottom":"0","left":"0","right":"0"}}}' \
61+
--output "$OUTPUT_DIR/06-tall-narrow.pdf"
62+
echo " -> $OUTPUT_DIR/06-tall-narrow.pdf"
63+
64+
# Test 7: Wide short PDF (landscape-like custom)
65+
echo "Test 7: Wide short PDF (400mm x 150mm)"
66+
curl -s -X POST "$BASE_URL" \
67+
-H "Content-Type: application/json" \
68+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"400mm","height":"150mm","margin":{"top":"0","bottom":"0","left":"0","right":"0"}}}' \
69+
--output "$OUTPUT_DIR/07-wide-short.pdf"
70+
echo " -> $OUTPUT_DIR/07-wide-short.pdf"
71+
72+
# Test 8: Small PDF (business card size)
73+
echo "Test 8: Business card size (85mm x 55mm)"
74+
curl -s -X POST "$BASE_URL" \
75+
-H "Content-Type: application/json" \
76+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"85mm","height":"55mm","margin":{"top":"0","bottom":"0","left":"0","right":"0"}}}' \
77+
--output "$OUTPUT_DIR/08-business-card.pdf"
78+
echo " -> $OUTPUT_DIR/08-business-card.pdf"
79+
80+
# Test 9: Large poster size
81+
echo "Test 9: Large poster (500mm x 700mm)"
82+
curl -s -X POST "$BASE_URL" \
83+
-H "Content-Type: application/json" \
84+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"500mm","height":"700mm","margin":{"top":"0","bottom":"0","left":"0","right":"0"}}}' \
85+
--output "$OUTPUT_DIR/09-poster.pdf"
86+
echo " -> $OUTPUT_DIR/09-poster.pdf"
87+
88+
# Test 10: Only width specified
89+
echo "Test 10: Only width specified (300mm width)"
90+
curl -s -X POST "$BASE_URL" \
91+
-H "Content-Type: application/json" \
92+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"300mm","margin":{"top":"0","bottom":"0","left":"0","right":"0"}}}' \
93+
--output "$OUTPUT_DIR/10-width-only.pdf"
94+
echo " -> $OUTPUT_DIR/10-width-only.pdf"
95+
96+
# Test 11: Only height specified
97+
echo "Test 11: Only height specified (500mm height)"
98+
curl -s -X POST "$BASE_URL" \
99+
-H "Content-Type: application/json" \
100+
-d '{"url":"https://example.com","type":"pdf","pdf":{"height":"500mm","margin":{"top":"0","bottom":"0","left":"0","right":"0"}}}' \
101+
--output "$OUTPUT_DIR/11-height-only.pdf"
102+
echo " -> $OUTPUT_DIR/11-height-only.pdf"
103+
104+
# Test 12: With HTML content - gradient background
105+
echo "Test 12: HTML content with gradient (150mm x 300mm)"
106+
curl -s -X POST "$BASE_URL" \
107+
-H "Content-Type: application/json" \
108+
-d '{"html":"<html><body style=\"background:linear-gradient(to bottom,#ff6b6b,#4ecdc4);height:100vh;display:flex;align-items:center;justify-content:center\"><h1 style=\"color:white;font-size:48px\">Custom Size</h1></body></html>","type":"pdf","pdf":{"width":"150mm","height":"300mm","margin":{"top":"0","bottom":"0","left":"0","right":"0"},"printBackground":true}}' \
109+
--output "$OUTPUT_DIR/12-html-gradient.pdf"
110+
echo " -> $OUTPUT_DIR/12-html-gradient.pdf"
111+
112+
# Test 13: With HTML content - table
113+
echo "Test 13: HTML table content (250mm x 150mm)"
114+
curl -s -X POST "$BASE_URL" \
115+
-H "Content-Type: application/json" \
116+
-d '{"html":"<html><body><table border=\"1\" style=\"width:100%;border-collapse:collapse\"><tr><th>Name</th><th>Value</th></tr><tr><td>Width</td><td>250mm</td></tr><tr><td>Height</td><td>150mm</td></tr></table></body></html>","type":"pdf","pdf":{"width":"250mm","height":"150mm","margin":{"top":"10mm","bottom":"10mm","left":"10mm","right":"10mm"}}}' \
117+
--output "$OUTPUT_DIR/13-html-table.pdf"
118+
echo " -> $OUTPUT_DIR/13-html-table.pdf"
119+
120+
# Test 14: Landscape with custom dimensions
121+
echo "Test 14: Landscape mode with custom (297mm x 210mm)"
122+
curl -s -X POST "$BASE_URL" \
123+
-H "Content-Type: application/json" \
124+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"297mm","height":"210mm","landscape":true,"margin":{"top":"0","bottom":"0","left":"0","right":"0"}}}' \
125+
--output "$OUTPUT_DIR/14-landscape-custom.pdf"
126+
echo " -> $OUTPUT_DIR/14-landscape-custom.pdf"
127+
128+
# Test 15: Mixed units (should still work)
129+
echo "Test 15: Width in inches, height in mm (5in x 200mm)"
130+
curl -s -X POST "$BASE_URL" \
131+
-H "Content-Type: application/json" \
132+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"5in","height":"200mm","margin":{"top":"0","bottom":"0","left":"0","right":"0"}}}' \
133+
--output "$OUTPUT_DIR/15-mixed-units.pdf"
134+
echo " -> $OUTPUT_DIR/15-mixed-units.pdf"
135+
136+
# Test 16: Receipt/ticket size
137+
echo "Test 16: Receipt/ticket size (80mm x 200mm)"
138+
curl -s -X POST "$BASE_URL" \
139+
-H "Content-Type: application/json" \
140+
-d '{"html":"<html><body style=\"font-family:monospace;padding:10px\"><h2>RECEIPT</h2><hr><p>Item 1.....$10.00</p><p>Item 2.....$15.00</p><hr><p><strong>Total: $25.00</strong></p></body></html>","type":"pdf","pdf":{"width":"80mm","height":"200mm","margin":{"top":"0","bottom":"0","left":"0","right":"0"}}}' \
141+
--output "$OUTPUT_DIR/16-receipt.pdf"
142+
echo " -> $OUTPUT_DIR/16-receipt.pdf"
143+
144+
# Test 17: Photo print size (4x6 inches)
145+
echo "Test 17: Photo print 4x6 inches"
146+
curl -s -X POST "$BASE_URL" \
147+
-H "Content-Type: application/json" \
148+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"4in","height":"6in","margin":{"top":"0","bottom":"0","left":"0","right":"0"}}}' \
149+
--output "$OUTPUT_DIR/17-photo-4x6.pdf"
150+
echo " -> $OUTPUT_DIR/17-photo-4x6.pdf"
151+
152+
# Test 18: Legal size
153+
echo "Test 18: Legal size (8.5in x 14in)"
154+
curl -s -X POST "$BASE_URL" \
155+
-H "Content-Type: application/json" \
156+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"8.5in","height":"14in","margin":{"top":"1in","bottom":"1in","left":"1in","right":"1in"}}}' \
157+
--output "$OUTPUT_DIR/18-legal.pdf"
158+
echo " -> $OUTPUT_DIR/18-legal.pdf"
159+
160+
# Test 19: A5 size custom
161+
echo "Test 19: A5 size (148mm x 210mm)"
162+
curl -s -X POST "$BASE_URL" \
163+
-H "Content-Type: application/json" \
164+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"148mm","height":"210mm","margin":{"top":"10mm","bottom":"10mm","left":"10mm","right":"10mm"}}}' \
165+
--output "$OUTPUT_DIR/19-a5-custom.pdf"
166+
echo " -> $OUTPUT_DIR/19-a5-custom.pdf"
167+
168+
# Test 20: A3 size custom
169+
echo "Test 20: A3 size (297mm x 420mm)"
170+
curl -s -X POST "$BASE_URL" \
171+
-H "Content-Type: application/json" \
172+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"297mm","height":"420mm","margin":{"top":"0","bottom":"0","left":"0","right":"0"}}}' \
173+
--output "$OUTPUT_DIR/20-a3-custom.pdf"
174+
echo " -> $OUTPUT_DIR/20-a3-custom.pdf"
175+
176+
# Test 21: Verify default A4 still works (no custom dimensions)
177+
echo "Test 21: Default A4 format (no custom dimensions)"
178+
curl -s -X POST "$BASE_URL" \
179+
-H "Content-Type: application/json" \
180+
-d '{"url":"https://example.com","type":"pdf","pdf":{"margin":{"top":"0","bottom":"0","left":"0","right":"0"}}}' \
181+
--output "$OUTPUT_DIR/21-default-a4.pdf"
182+
echo " -> $OUTPUT_DIR/21-default-a4.pdf"
183+
184+
# Test 22: Device viewport + custom PDF size
185+
echo "Test 22: Device viewport 800x600 + PDF 300mm x 200mm"
186+
curl -s -X POST "$BASE_URL" \
187+
-H "Content-Type: application/json" \
188+
-d '{"url":"https://example.com","type":"pdf","device":{"width":800,"height":600},"pdf":{"width":"300mm","height":"200mm","margin":{"top":"0","bottom":"0","left":"0","right":"0"}}}' \
189+
--output "$OUTPUT_DIR/22-viewport-custom-pdf.pdf"
190+
echo " -> $OUTPUT_DIR/22-viewport-custom-pdf.pdf"
191+
192+
# Test 23: With scale option
193+
echo "Test 23: Custom size with scale 0.8 (200mm x 280mm)"
194+
curl -s -X POST "$BASE_URL" \
195+
-H "Content-Type: application/json" \
196+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"200mm","height":"280mm","scale":0.8,"margin":{"top":"0","bottom":"0","left":"0","right":"0"}}}' \
197+
--output "$OUTPUT_DIR/23-with-scale.pdf"
198+
echo " -> $OUTPUT_DIR/23-with-scale.pdf"
199+
200+
# Test 24: With header and footer
201+
echo "Test 24: Custom size with header/footer (210mm x 297mm)"
202+
curl -s -X POST "$BASE_URL" \
203+
-H "Content-Type: application/json" \
204+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"210mm","height":"297mm","displayHeaderFooter":true,"headerTemplate":"<div style=\"font-size:10px;text-align:center;width:100%\">Header</div>","footerTemplate":"<div style=\"font-size:10px;text-align:center;width:100%\">Page <span class=\"pageNumber\"></span></div>","margin":{"top":"20mm","bottom":"20mm","left":"10mm","right":"10mm"}}}' \
205+
--output "$OUTPUT_DIR/24-header-footer.pdf"
206+
echo " -> $OUTPUT_DIR/24-header-footer.pdf"
207+
208+
# Test 25: Decimal values
209+
echo "Test 25: Decimal dimensions (123.5mm x 456.7mm)"
210+
curl -s -X POST "$BASE_URL" \
211+
-H "Content-Type: application/json" \
212+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"123.5mm","height":"456.7mm","margin":{"top":"0","bottom":"0","left":"0","right":"0"}}}' \
213+
--output "$OUTPUT_DIR/25-decimal.pdf"
214+
echo " -> $OUTPUT_DIR/25-decimal.pdf"
215+
216+
# Test 26: Tiny PDF (1cm x 1cm)
217+
echo "Test 26: Tiny PDF (1cm x 1cm)"
218+
curl -s -X POST "$BASE_URL" \
219+
-H "Content-Type: application/json" \
220+
-d '{"url":"https://example.com","type":"pdf","pdf":{"width":"1cm","height":"1cm","margin":{"top":"0","bottom":"0","left":"0","right":"0"}}}' \
221+
--output "$OUTPUT_DIR/26-tiny-1cm.pdf"
222+
echo " -> $OUTPUT_DIR/26-tiny-1cm.pdf"
223+
224+
225+
echo ""
226+
echo "=========================================="
227+
echo "All tests completed!"
228+
echo "=========================================="
229+
echo ""
230+
echo "Output files:"
231+
ls -la "$OUTPUT_DIR"/*.pdf 2>/dev/null | awk '{print $5, $9}' | column -t
232+
echo ""
233+
echo "To verify PDF dimensions on macOS:"
234+
echo " mdls -name kMDItemPageHeight -name kMDItemPageWidth $OUTPUT_DIR/01-custom-mm.pdf"
235+
echo ""
236+
echo "Or with pdfinfo (if installed):"
237+
echo " pdfinfo $OUTPUT_DIR/01-custom-mm.pdf | grep 'Page size'"

0 commit comments

Comments
 (0)