Last active
February 20, 2026 05:06
-
-
Save aspose-com-gists/5b0b1d215a411cd963fd8d0dcd4a4f4d to your computer and use it in GitHub Desktop.
Convert HTML Tables to PDF with Aspose.HTML in Python via .NET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from pathlib import Path | |
| from aspose.html import HTMLDocument | |
| from aspose.html.converters import Converter | |
| from aspose.html.saving import PdfSaveOptions | |
| def convert_table_html_to_pdf(input_html: str, output_pdf: str) -> None: | |
| Path(output_pdf).parent.mkdir(parents=True, exist_ok=True) | |
| pdf_options = PdfSaveOptions() | |
| with HTMLDocument(input_html) as document: | |
| Converter.convert_html(document, pdf_options, output_pdf) | |
| if __name__ == "__main__": | |
| convert_table_html_to_pdf( | |
| input_html="input/table.html", | |
| output_pdf="output/table.pdf" | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8" /> | |
| <title>Table Report</title> | |
| <style> | |
| :root { | |
| --border: #e5e7eb; | |
| --text: #111827; | |
| --muted: #6b7280; | |
| --header: #f3f4f6; | |
| --zebra: #fafafa; | |
| } | |
| body { | |
| margin: 0; | |
| padding: 24px; | |
| font-family: Arial, sans-serif; | |
| color: var(--text); | |
| } | |
| .title { | |
| font-size: 20px; | |
| font-weight: 700; | |
| margin: 0 0 6px 0; | |
| } | |
| .subtitle { | |
| font-size: 12px; | |
| color: var(--muted); | |
| margin: 0 0 16px 0; | |
| } | |
| table { | |
| width: 100%; | |
| border-collapse: collapse; | |
| table-layout: fixed; | |
| } | |
| thead th { | |
| background: var(--header); | |
| font-size: 12px; | |
| text-align: left; | |
| border: 1px solid var(--border); | |
| padding: 10px 8px; | |
| white-space: nowrap; | |
| } | |
| tbody td { | |
| font-size: 12px; | |
| border: 1px solid var(--border); | |
| padding: 10px 8px; | |
| vertical-align: top; | |
| word-wrap: break-word; | |
| overflow-wrap: break-word; | |
| } | |
| tbody tr:nth-child(even) td { | |
| background: var(--zebra); | |
| } | |
| .num { | |
| text-align: right; | |
| white-space: nowrap; | |
| } | |
| .small { | |
| font-size: 11px; | |
| color: var(--muted); | |
| margin-top: 10px; | |
| } | |
| /* PDF friendly rules for long tables */ | |
| tr, td, th { | |
| page-break-inside: avoid; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="title">Invoice Items</div> | |
| <div class="subtitle">Convert HTML tables to PDF while preserving layout and styling</div> | |
| <table aria-label="invoice items"> | |
| <thead> | |
| <tr> | |
| <th style="width: 44%;">Description</th> | |
| <th style="width: 10%;">Qty</th> | |
| <th style="width: 16%;">Unit Price</th> | |
| <th style="width: 16%;">Discount</th> | |
| <th style="width: 14%;">Total</th> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| <tr> | |
| <td> | |
| Annual subscription for document conversion services with support coverage. | |
| Includes priority handling for table heavy reports. | |
| </td> | |
| <td class="num">1</td> | |
| <td class="num">499.00</td> | |
| <td class="num">0.00</td> | |
| <td class="num">499.00</td> | |
| </tr> | |
| <tr> | |
| <td>Invoice rendering verification for long descriptions and wrapping behavior</td> | |
| <td class="num">3</td> | |
| <td class="num">35.00</td> | |
| <td class="num">5.00</td> | |
| <td class="num">100.00</td> | |
| </tr> | |
| <tr> | |
| <td>PDF export formatting for borders, zebra rows, and numeric alignment</td> | |
| <td class="num">2</td> | |
| <td class="num">60.00</td> | |
| <td class="num">0.00</td> | |
| <td class="num">120.00</td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| <div class="small">Tip: Use fixed column widths and numeric alignment for stable table output in PDF.</div> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment