The rendercv.renderer package contains the necessary classes and functions for
generating LATEX, PDF, Markdown, HTML, and PNG files from the RenderCVDataModel
object.
The LATEX and Markdown files are generated with
Jinja2 templates. Then, the LATEX
file is rendered into a PDF with TinyTeX, a LATEX
distribution. The Markdown file is rendered into an HTML file with markdown package.
The PDF files are rendered into PNG files with PyMuPDF/fitz package.
defcreate_a_latex_file(rendercv_data_model:data.RenderCVDataModel,output_directory:pathlib.Path)->pathlib.Path:"""Render the $\\LaTeX$ file with the given data model and write it to the output directory. Args: rendercv_data_model: The data model. output_directory: Path to the output directory. Returns: The path to the generated $\\LaTeX$ file. """# Create output directory if it doesn't exist:ifnotoutput_directory.is_dir():output_directory.mkdir(parents=True)jinja2_environment=templater.setup_jinja2_environment()latex_file_object=templater.LaTeXFile(rendercv_data_model,jinja2_environment,)latex_file_name=f"{str(rendercv_data_model.cv.name).replace(' ','_')}_CV.tex"latex_file_path=output_directory/latex_file_namelatex_file_object.create_file(latex_file_path)returnlatex_file_path
defcreate_a_latex_file_and_copy_theme_files(rendercv_data_model:data.RenderCVDataModel,output_directory:pathlib.Path)->pathlib.Path:"""Render the $\\LaTeX$ file with the given data model in the output directory and copy the auxiliary theme files to the output directory. Args: rendercv_data_model: The data model. output_directory: Path to the output directory. Returns: The path to the rendered $\\LaTeX$ file. """latex_file_path=create_a_latex_file(rendercv_data_model,output_directory)copy_theme_files_to_output_directory(rendercv_data_model.design.theme,output_directory)# Copy the profile picture to the output directory, if it exists:ifrendercv_data_model.cv.photo:shutil.copyfile(rendercv_data_model.cv.photo,output_directory/rendercv_data_model.cv.photo.name,)returnlatex_file_path
defcreate_a_markdown_file(rendercv_data_model:data.RenderCVDataModel,output_directory:pathlib.Path)->pathlib.Path:"""Render the Markdown file with the given data model and write it to the output directory. Args: rendercv_data_model: The data model. output_directory: Path to the output directory. Returns: The path to the rendered Markdown file. """# create output directory if it doesn't exist:ifnotoutput_directory.is_dir():output_directory.mkdir(parents=True)jinja2_environment=templater.setup_jinja2_environment()markdown_file_object=templater.MarkdownFile(rendercv_data_model,jinja2_environment,)markdown_file_name=f"{str(rendercv_data_model.cv.name).replace(' ','_')}_CV.md"markdown_file_path=output_directory/markdown_file_namemarkdown_file_object.create_file(markdown_file_path)returnmarkdown_file_path
defrender_a_pdf_from_latex(latex_file_path:pathlib.Path,local_latex_command:Optional[str]=None)->pathlib.Path:"""Run TinyTeX with the given $\\LaTeX$ file to render the PDF. Args: latex_file_path: The path to the $\\LaTeX$ file. Returns: The path to the rendered PDF file. """returnrendercv_tinytex.run_latex(latex_file_path,local_latex_command)
defrender_an_html_from_markdown(markdown_file_path:pathlib.Path)->pathlib.Path:"""Render an HTML file from a Markdown file with the same name and in the same directory. It uses `rendercv/themes/main.j2.html` as the Jinja2 template. Args: markdown_file_path: The path to the Markdown file. Returns: The path to the rendered HTML file. """# check if the file exists:ifnotmarkdown_file_path.is_file():message=f"The file {markdown_file_path} doesn't exist!"raiseFileNotFoundError(message)# Convert the markdown file to HTML:markdown_text=markdown_file_path.read_text(encoding="utf-8")html_body=markdown.markdown(markdown_text)# Get the title of the markdown content:title=re.search(r"# (.*)\n",markdown_text)title=title.group(1)iftitleelseNonejinja2_environment=templater.setup_jinja2_environment()html_template=jinja2_environment.get_template("main.j2.html")html=html_template.render(html_body=html_body,title=title)# Write html into a file:html_file_path=markdown_file_path.parent/f"{markdown_file_path.stem}.html"html_file_path.write_text(html,encoding="utf-8")returnhtml_file_path
defrender_pngs_from_pdf(pdf_file_path:pathlib.Path)->list[pathlib.Path]:"""Render a PNG file for each page of the given PDF file. Args: pdf_file_path: The path to the PDF file. Returns: The paths to the rendered PNG files. """# check if the file exists:ifnotpdf_file_path.is_file():message=f"The file {pdf_file_path} doesn't exist!"raiseFileNotFoundError(message)# convert the PDF to PNG:png_directory=pdf_file_path.parentpng_file_name=pdf_file_path.stempng_files=[]pdf=fitz.open(pdf_file_path)# open the PDF fileforpageinpdf:# iterate the pagesimage=page.get_pixmap(dpi=300)# type: ignorepng_file_path=png_directory/f"{png_file_name}_{page.number+1}.png"# type: ignoreimage.save(png_file_path)png_files.append(png_file_path)returnpng_files