borb + plotly
Anonymous
python
06/23/2023 2:18 PM
1.9 KB
28
Indexable
import io
from borb.pdf import Document
from borb.pdf import Page
from borb.pdf import SingleColumnLayout
from borb.pdf import PageLayout
from borb.pdf import PDF
from borb.pdf import Image
import plotly.graph_objects as go
from PIL import Image as pImage
from decimal import Decimal
def create_figure() -> go.Figure:
# Create the data for the figure
x = [1, 2, 3, 4, 5]
y = [1, 4, 3, 2, 5]
# Create a scatter plot figure
fig = go.Figure(data=go.Scatter(x=x, y=y, mode='markers'))
# Update the layout of the figure
fig.update_layout(
title='Scatter Plot',
xaxis=dict(title='X-axis'),
yaxis=dict(title='Y-axis'),
showlegend=False
)
# return
return fig
def main():
# create Document
doc: Document = Document()
from pathlib import Path
# create Page
page: Page = Page()
# add Page to Document
doc.add_page(page)
# set a PageLayout
layout: PageLayout = SingleColumnLayout(page)
layout.vertical_margin = page.get_page_info().get_height() * Decimal(0.02)
# build a (plotly) Figure
fig: go.Figure = create_figure()
# add an Image
layout.add(
Image(
pImage.open(io.BytesIO(fig.to_image(format="png"))),
width=Decimal(278),
height=Decimal(238),
)
)
# store
with open("test.pdf", "wb") as pdf_file_handle:
PDF.dumps(pdf_file_handle, doc)
if __name__ == "__main__":
main()Editor is loading...