Commit
·
a2c2207
1
Parent(s):
fe2626e
added url to pdf
Browse files- webrify2.py +38 -1
webrify2.py
CHANGED
|
@@ -6,6 +6,9 @@ import base64
|
|
| 6 |
import json
|
| 7 |
import asyncio
|
| 8 |
from playwright.async_api import async_playwright, TimeoutError as PlaywrightTimeoutError
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI(title="Web Analyzer API")
|
| 11 |
|
|
@@ -398,4 +401,38 @@ async def accessibility_check(url: str):
|
|
| 398 |
}
|
| 399 |
finally:
|
| 400 |
await browser.close()
|
| 401 |
-
await pw.stop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import json
|
| 7 |
import asyncio
|
| 8 |
from playwright.async_api import async_playwright, TimeoutError as PlaywrightTimeoutError
|
| 9 |
+
from fastapi.responses import FileResponse
|
| 10 |
+
import os
|
| 11 |
+
import uuid
|
| 12 |
|
| 13 |
app = FastAPI(title="Web Analyzer API")
|
| 14 |
|
|
|
|
| 401 |
}
|
| 402 |
finally:
|
| 403 |
await browser.close()
|
| 404 |
+
await pw.stop()
|
| 405 |
+
|
| 406 |
+
|
| 407 |
+
|
| 408 |
+
|
| 409 |
+
@app.get("/html-to-pdf")
|
| 410 |
+
async def convert_html_to_pdf(url: str):
|
| 411 |
+
from playwright.async_api import async_playwright
|
| 412 |
+
|
| 413 |
+
filename = f"{uuid.uuid4().hex}.pdf"
|
| 414 |
+
output_path = f"/tmp/{filename}" # Or use another temp dir
|
| 415 |
+
|
| 416 |
+
pw = await async_playwright().start()
|
| 417 |
+
browser = await pw.chromium.launch()
|
| 418 |
+
page = await browser.new_page()
|
| 419 |
+
|
| 420 |
+
try:
|
| 421 |
+
await page.goto(url, wait_until="networkidle")
|
| 422 |
+
await page.pdf(
|
| 423 |
+
path=output_path,
|
| 424 |
+
format="A4",
|
| 425 |
+
print_background=True,
|
| 426 |
+
margin={"top": "1cm", "bottom": "1cm", "left": "1cm", "right": "1cm"},
|
| 427 |
+
)
|
| 428 |
+
finally:
|
| 429 |
+
await browser.close()
|
| 430 |
+
await pw.stop()
|
| 431 |
+
|
| 432 |
+
# Serve the file and remove after response
|
| 433 |
+
return FileResponse(
|
| 434 |
+
path=output_path,
|
| 435 |
+
filename="webpage.pdf",
|
| 436 |
+
media_type="application/pdf",
|
| 437 |
+
headers={"Content-Disposition": "attachment; filename=webpage.pdf"}
|
| 438 |
+
)
|