Skip to content

Commit b2f8c9e

Browse files
Update typst_pdf_renderer (#1869)
Update typst_pdf_renderer Made in [Fused Workbench](https://www.fused.io/workbench) Co-authored-by: milind@fused.io <milind@fused.io>
1 parent b11cf90 commit b2f8c9e

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<!--fused:readme-->
2+
Exported from Fused UDF Workbench
3+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"version": "0.0.3",
3+
"job_config": {
4+
"version": "0.0.3",
5+
"name": null,
6+
"steps": [
7+
{
8+
"type": "udf",
9+
"udf": {
10+
"type": "geopandas_v2",
11+
"name": "typst_pdf_renderer",
12+
"entrypoint": "udf",
13+
"parameters": {},
14+
"metadata": {
15+
"fused:name": "typst_pdf_renderer",
16+
"fused:slug": "typst_pdf_renderer",
17+
"fused:vizConfig": {
18+
"tileLayer": {
19+
"@@type": "TileLayer",
20+
"minZoom": 0,
21+
"maxZoom": 19,
22+
"tileSize": 256
23+
},
24+
"rasterLayer": {
25+
"@@type": "BitmapLayer",
26+
"pickable": true
27+
},
28+
"vectorLayer": {
29+
"@@type": "GeoJsonLayer",
30+
"stroked": true,
31+
"filled": false,
32+
"pickable": true,
33+
"lineWidthMinPixels": 1,
34+
"pointRadiusMinPixels": 1,
35+
"getLineColor": {
36+
"@@function": "colorContinuous",
37+
"attr": "value",
38+
"domain": [
39+
0,
40+
10
41+
],
42+
"steps": 20,
43+
"colors": "Burg",
44+
"nullColor": [
45+
184,
46+
184,
47+
184
48+
]
49+
},
50+
"getFillColor": [
51+
208,
52+
208,
53+
208,
54+
40
55+
]
56+
}
57+
},
58+
"fused:udfType": "auto"
59+
},
60+
"source": "typst_pdf_renderer.py",
61+
"headers": []
62+
}
63+
}
64+
],
65+
"metadata": null
66+
}
67+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
@fused.udf
2+
def udf(typst_content: str = """
3+
#set page(width: 400pt, height: auto, margin: 20pt)
4+
#set text(font: "Linux Libertine", size: 12pt)
5+
6+
= Hello from Typst!
7+
8+
This PDF was generated using *Typst* inside a Fused UDF.
9+
10+
== Features
11+
- Fast compilation
12+
- Modern syntax
13+
- Beautiful typography
14+
15+
#table(
16+
columns: (auto, auto),
17+
[*Name*], [*Value*],
18+
[Alpha], [1],
19+
[Beta], [2],
20+
[Gamma], [3],
21+
)
22+
23+
$ sum_(k=0)^n k = (n(n+1)) / 2 $
24+
"""):
25+
26+
html = typst_to_html(typst_content)
27+
return html
28+
29+
30+
def typst_to_html(content: str) -> str:
31+
"""Compile Typst content to PDF and return HTML to render it."""
32+
import sys
33+
sys.path.append("/mount/envs/milind/lib/python3.11/site-packages/")
34+
import typst
35+
import base64
36+
37+
# Compile typst to PDF
38+
typ_path = fused.file_path("document.typ")
39+
pdf_path = fused.file_path("document.pdf")
40+
41+
with open(typ_path, "w") as f:
42+
f.write(content)
43+
44+
typst.compile(typ_path, output=pdf_path)
45+
46+
with open(pdf_path, "rb") as f:
47+
pdf_bytes = f.read()
48+
49+
print(f"PDF generated: {len(pdf_bytes)} bytes")
50+
51+
# Convert to base64 and render as HTML
52+
pdf_base64 = base64.b64encode(pdf_bytes).decode('utf-8')
53+
54+
return f"""
55+
<html>
56+
<head>
57+
<style>
58+
body {{
59+
margin: 0;
60+
padding: 0;
61+
background: white;
62+
}}
63+
iframe {{
64+
width: 100%;
65+
height: 100vh;
66+
border: none;
67+
}}
68+
</style>
69+
</head>
70+
<body>
71+
<iframe src="data:application/pdf;base64,{pdf_base64}"></iframe>
72+
</body>
73+
</html>
74+
"""

0 commit comments

Comments
 (0)