0

I have running a Flask Application with embedded Dash App. The application code is entirely written in python that renders html code. Now, I'd like to be able to export certain <html> elements to pdf.

python code for generating html:

import dash
from dash.dependencies import Input, Output, State
from dash import dcc
from dash import html, dash_table
import dash_bootstrap_components as dbc

html.Div(
    [
        dbc.Row(
            [
                dbc.Card(
                    [
                        dbc.CardHeader("Median"),
                        dbc.CardBody(
                            [
                                html.P(id="card"),
                            ]
                        ),
                    ],
                    id="stat",
                    color="light",
                ),
            ]
        )
    ]
)    

Here's the html generated by python code:

<div class="row">
    
  <div id="stat" class="card bg-light">
    <div class="card-header">Median</div>
  </div>
    
  <div class="card-body">
    <p id="card"></p>
  </div>
    
</div>

Is there a pythonic way of exporting html to pdf? I can also write Javascript if there is a way.

Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63
kms
  • 1,810
  • 1
  • 41
  • 92

1 Answers1

0

1. Info on Dash => Html:

How to export a plotly dashboard app into a html standalone file to share with the others?

2. Html => Pdf:

2.1 pdfkit

import pdfkit
pdfkit.from_string('Shaurya GFG','GfG.pdf')

https://www.geeksforgeeks.org/python-convert-html-pdf/

https://github.com/JazzCore/python-pdfkit

2.2 xhtml2pdf

from xhtml2pdf import pisa            

result_file = open("test.pdf", "w+b")
pisa.CreatePDF(
  "<html><body><p>To PDF or not to PDF</p></body></html>",
  dest=result_file
)

https://pypi.org/project/xhtml2pdf/

3.3 selenium

Using the print feature of a browser and a pdf printer driver might also be an option:

how to save opened page as pdf in Selenium (Python)

Stefan
  • 10,010
  • 7
  • 61
  • 117