Пример #1
0
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.'))
Пример #2
0
 def to_latex(self, stylesheet_url="", settings=latex.DEFAULT_LATEX_OVERRIDES, *args, **kwargs):
     """
     Convert a document tree to LaTeX format, returning a string and a list.
     
     The ``to_latex`` method will return a string containing the produced
     LaTeX file and a list containing the locations of temporary files
     created during the LaTeX conversion. The LaTeX string returned will
     most likely be a ``unicode``, and the list of files will be (possibly)
     a .tex file (the stylesheet) followed by several image files.
     
     The LaTeX stylesheet is passed in the ``stylesheet_url`` keyword. The
     way of dealing with the stylesheet is quite complex, so here's a
     summary of the possibile configurations:
         
         1)  You pass a string which *is* the stylesheet, equivalent to the
             results of an ``open('filename').read()``. In this case, a .tex
             file *will* be included in the list of temporary files.
         
         2)  You pass a filename *pointing to* the stylesheet, equivalent to
             the last example's ``'filename'``. This would *not* create a
             .tex file in the temporary file list.
         
         3)  You pass a file-like object which *contains* the stylesheet,
             which is the same as passing ``open('filename')``. As with
             example 1, a .tex file *will* be included in the returned list
             of temporary files.
     
     The ``settings`` keyword argument specifies a particular dictionary of
     docutils writer settings to be passed to the LaTeX writer. They will
     augment the ``ReSTDocument`` instance's ``default_settings`` attribute,
     but keywords specified in ``settings`` will override any conflicts in
     the instance-specific settings. This defaults to the
     ``DEFAULT_LATEX_OVERRIDES`` dictionary, in the ``rst2a.latex`` module.
     
     Any additional positional or keyword arguments will be passed to the
     ``rst2a.latex.doctree_to_latex`` function.
     """
     # Create a set of conversion settings based on those given and the
     # default settings held in the ``ReSTDocument`` instance.
     conversion_settings = copy(self.default_settings)
     conversion_settings.update(settings)
     # Wrap the ``latex.doctree_to_latex`` function.
     return latex.doctree_to_latex(
         self.document, self.img_localizer, stylesheet_url=stylesheet_url, settings=conversion_settings
     )