def doctree_to_pdf(doctree, img_localizer, stylesheet_url='', settings=latex.DEFAULT_LATEX_OVERRIDES, *args, **kwargs): if not pdflatex_installed(): raise EnvironmentError((127, 'pdflatex not installed.')) cleanup_stylesheet = False latex_string, extra_files = latex.doctree_to_latex(doctree, img_localizer, settings=settings, stylesheet_url=stylesheet_url, *args, **kwargs) if os.path.splitext(extra_files[0])[1] == '.tex': cleanup_stylesheet = True temp_pdf_dir = mkdtemp() temp_latex_filename = common.create_temp_file(latex_string, suffix='.tex', dir=temp_pdf_dir) err = call_pdflatex_repeat(3, temp_latex_filename, pdf_dir=temp_pdf_dir) if err == 0: pdf_conversion_successful = True else: pdf_conversion_successful = False img_localizer.cleanup_images(doctree) if cleanup_stylesheet: os.remove(extra_files[0]) if pdf_conversion_successful: pdf_prefix = os.path.splitext(temp_latex_filename)[0] pdf_filename = pdf_prefix + os.extsep + 'pdf' pdf_handle = open(pdf_filename) pdf_data = pdf_handle.read() pdf_handle.close() common.remove_dir(temp_pdf_dir) return pdf_data else: common.remove_dir(temp_pdf_dir) raise EnvironmentError((err, 'PDF conversion unsuccessful.'))
def doctree_to_latex(doctree, img_localizer, stylesheet_url="", settings=DEFAULT_LATEX_OVERRIDES, *args, **kwargs): cleanup_stylesheet = False if not isfile(str(stylesheet_url)) or is_filelike(stylesheet_url): stylesheet_url = create_temp_file(stylesheet_url, suffix=".tex") cleanup_stylesheet = True conversion_settings = copy(settings) conversion_settings["stylesheet-path"] = stylesheet_url img_localizer.localize_images(doctree) latex_string = publish_from_doctree( doctree, writer_name="latex", settings_overrides=conversion_settings, *args, **kwargs ) temp_files = sorted(list(set(img_localizer.values()))) if cleanup_stylesheet: temp_files = [stylesheet_path] + temp_files return latex_string, temp_files