Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Request Feature] Native pdf export option #23

Open
GJoe2 opened this issue Aug 28, 2024 · 3 comments
Open

[Request Feature] Native pdf export option #23

GJoe2 opened this issue Aug 28, 2024 · 3 comments

Comments

@GJoe2
Copy link

GJoe2 commented Aug 28, 2024

While html is fine and could be printed manually to pdf format , a different story is doing recursively . There are some python package that can help with that but there are some major drawbacks:

  1. pdfkit / weasyprint : Simple and effective way to export html files to pdf but does not support latex expression or MathML which are key components in the html file made by efficalc

  2. playwright/ pyppeteer: It runs a headless browser from which one can write necessary steps to print the html file to pdf. At first glance it looks a complete solution but a little overkill download an entire browser just to export an html file to pdf. Also I have issues at running with a Jupyter notebook which is my main tool to write code because of nested async loops. In resume, a little more complex and present issues that I shouldn't focus to work on.

So, I suggest to implement a class to export the html files into pdf. Or maybe just a clarification in regards of pdf printing programmatically. Thanks in regards

@youandvern
Copy link
Owner

Hey, thanks for raising this feature request and for outlining some of the options.

You bring up some great points. I’m hesitant to introduce a class that specifically depends on a particular library, as that would impose additional installation requirements on the end-user. Both pdfkit and weasyprint have non-python dependencies.

Instead of implementing a solution based on a specific library, I’m considering an approach that involves pre-rendering the equations as images within the HTML report. This would allow for seamless HTML-to-PDF conversion using any of the tools you mentioned.

By generating a pre-rendered HTML file, users get the flexibility to choose their preferred PDF converter for their workflow, without adding external dependencies to this library.

Would that be an acceptable solution for you?

@GJoe2
Copy link
Author

GJoe2 commented Aug 30, 2024

That sounds great! I would advice to leave an option to not pre-render for people which uses cases may need a high quality document or just a clean html file.

I'm closing this issue but feel free to add anything else or comments your progress.

A little offtopic, but I manage to create a routine which uses playwright to render math expression in html files, maybe someone else may need it

import asyncio
from playwright.async_api import async_playwright

html_file_path = r"report.html"
output_pdf_path = r"report.pdf"

async def export_html_to_pdf(html_path, pdf_path):
    async with async_playwright() as p:
        browser = await p.chromium.launch(executable_path=r'C:\path_to_chrome\chrome.exe', headless=True)
        page = await browser.new_page()

        with open(html_path, "r", encoding="ISO-8859-1") as file:
            html_content = file.read()

        await page.set_content(html_content)

        await page.wait_for_function("window.MathJax && window.MathJax.typesetPromise")

        await page.pdf(path=pdf_path, format='A4')

        await browser.close()
        print(f"PDF successfully created at: {pdf_path}")

asyncio.run(export_html_to_pdf(html_file_path, output_pdf_path))

It does the job but may take some time if you want to convert files in hundreds

@GJoe2 GJoe2 closed this as completed Aug 30, 2024
@youandvern
Copy link
Owner

Hey, thanks for that. I'll definitely make the pre-rendering configurable so that reports can be generated either way.

I'm going to leave this open so I have a reminder and action item to fix this. I'll close the issue when the PR merges with this change.

Thanks

@youandvern youandvern reopened this Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants