-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat:add latex to pdf in FileWriteToolkit #2357
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
Conversation
Thanks! quick question, does this also compile on windows with no errors? used to run into bit of complexities with this library on win |
thanks @JINO-ROHIT ,i havent test on windows,cause im using macos right now. and i have already tested it on macos,it needs the users to install the latex compile before using this,i will test on windows also,please letme know your detailed problems when runing this in windows |
of course, ill help test this on my windows machine |
thanks @JINO-ROHIT ! |
hii @zjrwtx , just tested a bit, so for windows installating pylatex alone doesnt work, theres quite a bit of complication 'steps' you had to do to get it working on windows, attaching this manual - https://latextools.readthedocs.io/en/latest/install/#windows
|
thanks for your help @JINO-ROHIT .in macos ,after installing the pylatex,we also need to install some compile latex enginee ,i guess we could let the user know about this. what do you think? cc @Wendong-Fan @MuggleJinx |
thanks @JINO-ROHIT and @zjrwtx , It would be better if we can make it simpler for the user, would https://github.com/rstudio/tinytex and https://www.gnu.org/software/groff/ be helpful? |
@@ -148,40 +148,73 @@ def _write_docx_file(self, file_path: Path, content: str) -> None: | |||
document.save(str(file_path)) | |||
logger.debug(f"Wrote DOCX to {file_path} with default formatting") | |||
|
|||
def _write_pdf_file(self, file_path: Path, content: str, **kwargs) -> None: | |||
def _write_pdf_file( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To improve, we can add error handling:
def _write_pdf_file( | |
try: | |
# PDF generation code | |
except ImportError as e: | |
raise RuntimeError(f"Required library not installed: {e}") | |
except Exception as e: | |
raise RuntimeError(f"Failed to generate PDF: {e}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rest looks cool to me.
print( | ||
"Example 7: Please give me a pdf file with formulas of advanced mathematics" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
print( | |
"Example 7: Please give me a pdf file with formulas of advanced mathematics" | |
) | |
print(f"Example 7: {latex_pdf_query}) |
|
||
camel_agent.reset() | ||
response = camel_agent.step(pdf_query) | ||
print("Example 7: what is multiagent?please export them as pdf file") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
print("Example 7: what is multiagent?please export them as pdf file") | |
print(f"Example 8: {pdf_query}") |
|
||
|
||
# Example 8: what is multiagent?please export them as pdf file | ||
pdf_query = """what is multiagent?please export them as pdf file""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pdf_query = """what is multiagent?please export them as pdf file""" | |
pdf_query = """what is multiagent? Please export them as pdf file""" |
@@ -148,40 +148,73 @@ def _write_docx_file(self, file_path: Path, content: str) -> None: | |||
document.save(str(file_path)) | |||
logger.debug(f"Wrote DOCX to {file_path} with default formatting") | |||
|
|||
def _write_pdf_file(self, file_path: Path, content: str, **kwargs) -> None: | |||
def _write_pdf_file( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add dependencies_required
decorator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @zjrwtx! It would be cool to record a demo on this!
thanks @lightaime ,i will do that. |
thanks @MuggleJinx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
camel/toolkits/file_write_toolkit.py
Outdated
""" | ||
from fpdf import FPDF | ||
if use_latex: | ||
from pylatex import ( # type: ignore[import-not-found, import-untyped] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why skip mypy test here, should we add pylatex to pyproject.toml?
camel/toolkits/file_write_toolkit.py
Outdated
use_latex (bool): Whether to use LaTeX for rendering . | ||
(requires LaTeX toolchain) | ||
If False, uses FPDF for simpler PDF generation. | ||
(default: False) | ||
|
||
Raises: | ||
RuntimeError: If the 'fpdf' library is not installed. | ||
RuntimeError: If the 'pylatex' library is not installed | ||
when use_latex=True. | ||
RuntimeError: If the 'fpdf' library is not installed | ||
when use_latex=False. | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
docstring format
use_latex (bool): Whether to use LaTeX for rendering. (requires
LaTeX toolchain). If False, uses FPDF for simpler PDF
generation. (default: :obj: `False`)
Raises:
RuntimeError: If the 'pylatex' or 'fpdf' library is not installed
when use_latex=True.
"""
|
||
# Example 7: Please give me a pdf file with formulas of advanced mathematics | ||
latex_pdf_query = ( | ||
"""Please give me a pdf file with formulas of advanced mathematics.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"""Please give me a pdf file with formulas of advanced mathematics.""" | |
"""Please give me a pdf file with formulas of advanced mathematics.""" |
camel/toolkits/file_write_toolkit.py
Outdated
pdf.multi_cell(0, line_height, para) | ||
else: | ||
pdf.ln(line_height) # Add empty line | ||
logger.debug(f"Wrote PDF (with LaTeX) to {file_path}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logger.debug(f"Wrote PDF (with LaTeX) to {file_path}") | |
logger.debug(f"Wrote PDF (with LaTeX) to {file_path}") |
camel/toolkits/file_write_toolkit.py
Outdated
if '$' in line: | ||
doc.append(Math(data=line.strip('$'))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here seems could be buggy if a line contains $ but is not a simple math expression enclosed by $, like"Price: $10")
Description
close #1908
Checklist
Go over all the following points, and put an
x
in all the boxes that apply.Fixes #issue-number
in the PR description (required)pyproject.toml
anduv lock
If you are unsure about any of these, don't hesitate to ask. We are here to help!