def translate(self): visitor = PDFTranslator(self.document, self.builder) self.document.walkabout(visitor) lang = self.config.language or 'en' langmod = get_language_available(lang)[2] self.docutils_languages = {lang: langmod} # Generate Contents topic manually if self.use_toc: contents=nodes.topic(classes=['contents']) contents+=nodes.title('') contents[0]+=nodes.Text(langmod.labels['contents']) contents['ids']=['Contents'] pending=nodes.topic() contents.append(pending) pending.details={} self.document.insert(0,nodes.raw(text='SetPageCounter 1 arabic', format='pdf')) self.document.insert(0,nodes.raw(text='OddPageBreak %s'%self.page_template, format='pdf')) self.document.insert(0,contents) self.document.insert(0,nodes.raw(text='SetPageCounter 1 lowerroman', format='pdf')) contTrans=PDFContents(self.document) contTrans.toc_depth = self.toc_depth contTrans.startnode=pending contTrans.apply() if self.use_coverpage: # Generate cover page # FIXME: duplicate from createpdf, refactor! # Add the Sphinx template paths def add_template_path(path): return os.path.join(self.srcdir, path) jinja_env = jinja2.Environment( loader=jinja2.FileSystemLoader([ self.srcdir, os.path.expanduser('~/.rst2pdf'), os.path.join(self.PATH,'templates')] + list(map(add_template_path, self.config.templates_path))), autoescape=jinja2.select_autoescape(['html', 'xml']) ) try: template = jinja_env.get_template(self.config.pdf_cover_template) except jinja2.TemplateNotFound: log.error("Can't find cover template %s, using default"%self.config.pdf_cover_template) template = jinja_env.get_template('sphinxcover.tmpl') # This is what's used in the python docs because # Latex does a manual linebreak. This sucks. authors=self.document.settings.author.split('\\') # Honour the "today" config setting if self.config.today : date = self.config.today else: date=time.strftime(self.config.today_fmt or _('%B %d, %Y')) # Feed data to the template, get restructured text. cover_text = template.render( title=self.document.settings.title or visitor.elements['title'], subtitle='%s %s'%(_('version'),self.config.version), authors=authors, date=date ) cover_tree = docutils.core.publish_doctree(cover_text) self.document.insert(0, cover_tree) sio=BytesIO() if self.invariant: createpdf.patch_PDFDate() createpdf.patch_digester() createpdf.RstToPdf(sphinx=True, stylesheets=self.stylesheets, language=self.__language, breaklevel=self.breaklevel, breakside=self.breakside, fit_mode=self.fitmode, font_path=self.fontpath, inline_footnotes=self.inline_footnotes, highlightlang=self.highlightlang, splittables=self.splittables, style_path=self.style_path, repeat_table_rows=self.repeat_table_rows, basedir=self.srcdir, def_dpi=self.default_dpi, real_footnotes=self.real_footnotes, numbered_links=self.use_numbered_links, background_fit_mode=self.fit_background_mode, baseurl=self.baseurl, section_header_depth=self.section_header_depth ).createPdf(doctree=self.document, output=sio, compressed=self.compressed) self.output=sio.getvalue()
def translate(self): visitor = PDFTranslator(self.document, self.builder) self.document.walkabout(visitor) lang = self.config.language or 'en' langmod = get_language_available(lang)[2] self.docutils_languages = {lang: langmod} # Generate Contents topic manually if self.use_toc: contents = nodes.topic(classes=['contents']) contents += nodes.title('') contents[0] += nodes.Text(langmod.labels['contents']) contents['ids'] = ['Contents'] pending = nodes.topic() contents.append(pending) pending.details = {} self.document.insert( 0, nodes.raw(text='SetPageCounter 1 arabic', format='pdf')) self.document.insert( 0, nodes.raw(text='OddPageBreak %s' % self.page_template, format='pdf')) self.document.insert(0, contents) self.document.insert( 0, nodes.raw(text='SetPageCounter 1 lowerroman', format='pdf')) contTrans = PDFContents(self.document) contTrans.toc_depth = self.toc_depth contTrans.startnode = pending contTrans.apply() if self.use_coverpage: # Generate cover page # FIXME: duplicate from createpdf, refactor! # Find cover template, save it in cover_file def find_cover(name): cover_path = [ self.srcdir, os.path.expanduser('~/.rst2pdf'), os.path.join(self.PATH, 'templates') ] # Add the Sphinx template paths def add_template_path(path): return os.path.join(self.srcdir, path) cover_path.extend( map(add_template_path, self.config.templates_path)) cover_file = None for d in cover_path: if os.path.exists(os.path.join(d, name)): cover_file = os.path.join(d, name) break return cover_file cover_file = find_cover(self.config.pdf_cover_template) if cover_file is None: log.error("Can't find cover template %s, using default" % self.custom_cover) cover_file = find_cover('sphinxcover.tmpl') # This is what's used in the python docs because # Latex does a manual linebreak. This sucks. authors = self.document.settings.author.split('\\') # Feed data to the template, get restructured text. cover_text = createpdf.renderTemplate( tname=cover_file, title=self.document.settings.title or visitor.elements['title'], subtitle='%s %s' % (_('version'), self.config.version), authors=authors, date=ustrftime(self.config.today_fmt or _('%B %d, %Y'))) cover_tree = docutils.core.publish_doctree(cover_text) self.document.insert(0, cover_tree) sio = StringIO() if self.invariant: createpdf.patch_PDFDate() createpdf.patch_digester() createpdf.RstToPdf(sphinx=True, stylesheets=self.stylesheets, language=self.__language, breaklevel=self.breaklevel, breakside=self.breakside, fit_mode=self.fitmode, font_path=self.fontpath, inline_footnotes=self.inline_footnotes, highlightlang=self.highlightlang, splittables=self.splittables, style_path=self.style_path, basedir=self.srcdir, def_dpi=self.default_dpi, real_footnotes=self.real_footnotes, numbered_links=self.use_numbered_links, background_fit_mode=self.fit_background_mode, baseurl=self.baseurl, section_header_depth=self.section_header_depth, floating_images=self.floating_images).createPdf( doctree=self.document, output=sio, compressed=self.compressed) self.output = sio.getvalue()
def translate(self): visitor = PDFTranslator(self.document, self.builder) self.document.walkabout(visitor) lang = self.config.language or 'en' langmod = get_language_available(lang)[2] self.docutils_languages = {lang: langmod} # Generate Contents topic manually if self.use_toc: contents=nodes.topic(classes=['contents']) contents+=nodes.title('') contents[0]+=nodes.Text(langmod.labels['contents']) contents['ids']=['Contents'] pending=nodes.topic() contents.append(pending) pending.details={} self.document.insert(0,nodes.raw(text='SetPageCounter 1 arabic', format='pdf')) self.document.insert(0,nodes.raw(text='OddPageBreak %s'%self.page_template, format='pdf')) self.document.insert(0,contents) self.document.insert(0,nodes.raw(text='SetPageCounter 1 lowerroman', format='pdf')) contTrans=PDFContents(self.document) contTrans.toc_depth = self.toc_depth contTrans.startnode=pending contTrans.apply() if self.use_coverpage: # Generate cover page # FIXME: duplicate from createpdf, refactor! # Find cover template, save it in cover_file def find_cover(name): cover_path=[self.srcdir, os.path.expanduser('~/.rst2pdf'), os.path.join(self.PATH,'templates')] # Add the Sphinx template paths def add_template_path(path): return os.path.join(self.srcdir, path) cover_path.extend(map(add_template_path, self.config.templates_path)) cover_file=None for d in cover_path: if os.path.exists(os.path.join(d,name)): cover_file=os.path.join(d,name) break return cover_file cover_file=find_cover(self.config.pdf_cover_template) if cover_file is None: log.error("Can't find cover template %s, using default"%self.custom_cover) cover_file=find_cover('sphinxcover.tmpl') # This is what's used in the python docs because # Latex does a manual linebreak. This sucks. authors=self.document.settings.author.split('\\') # Feed data to the template, get restructured text. cover_text = createpdf.renderTemplate(tname=cover_file, title=self.document.settings.title or visitor.elements['title'], subtitle='%s %s'%(_('version'),self.config.version), authors=authors, date=ustrftime(self.config.today_fmt or _('%B %d, %Y')) ) cover_tree = docutils.core.publish_doctree(cover_text) self.document.insert(0, cover_tree) sio=StringIO() if self.invariant: createpdf.patch_PDFDate() createpdf.patch_digester() createpdf.RstToPdf(sphinx=True, stylesheets=self.stylesheets, language=self.__language, breaklevel=self.breaklevel, breakside=self.breakside, fit_mode=self.fitmode, font_path=self.fontpath, inline_footnotes=self.inline_footnotes, highlightlang=self.highlightlang, splittables=self.splittables, style_path=self.style_path, basedir=self.srcdir, def_dpi=self.default_dpi, real_footnotes=self.real_footnotes, numbered_links=self.use_numbered_links, background_fit_mode=self.fit_background_mode, baseurl=self.baseurl, section_header_depth=self.section_header_depth ).createPdf(doctree=self.document, output=sio, compressed=self.compressed) self.output=sio.getvalue()
def translate(self): visitor = PDFTranslator(self.document, self.builder) self.document.walkabout(visitor) if self.config.language: langmod = languages.get_language(self.config.language[:2]) else: langmod = languages.get_language('en') # Generate Contents topic manually contents=nodes.topic(classes=['contents']) contents+=nodes.title('') contents[0]+=nodes.Text( langmod.labels['contents']) contents['ids']=['Contents'] pending=nodes.topic() contents.append(pending) pending.details={} self.document.insert(0,nodes.raw(text='SetPageCounter 1 arabic', format='pdf')) self.document.insert(0,nodes.raw(text='OddPageBreak %s'%self.page_template, format='pdf')) self.document.insert(0,contents) self.document.insert(0,nodes.raw(text='SetPageCounter 1 lowerroman', format='pdf')) contTrans=PDFContents(self.document) contTrans.startnode=pending contTrans.apply() if self.config.pdf_use_coverpage: # Generate cover page spacer=docutils.core.publish_doctree('.. raw:: pdf\n\n Spacer 0 3cm\n\n')[0] doctitle=nodes.title() doctitle.append(nodes.Text(self.document.settings.title or visitor.elements['title'])) docsubtitle=nodes.subtitle() docsubtitle.append(nodes.Text('%s %s'%(_('version'),self.config.version))) # This is what's used in the python docs because # Latex does a manual linrebreak. This sucks. authors=self.document.settings.author.split('\\') authornodes=[] for author in authors: node=nodes.paragraph() node.append(nodes.Text(author)) node['classes']=['author'] authornodes.append(node) date=nodes.paragraph() date.append(nodes.Text(ustrftime(self.config.today_fmt or _('%B %d, %Y')))) date['classes']=['author'] self.document.insert(0,nodes.raw(text='OddPageBreak %s'%self.page_template, format='pdf')) self.document.insert(0,date) self.document.insert(0,spacer) for node in authornodes[::-1]: self.document.insert(0,node) self.document.insert(0,spacer) self.document.insert(0,docsubtitle) self.document.insert(0,doctitle) sio=StringIO() if self.invariant: createpdf.patch_PDFDate() createpdf.patch_digester() createpdf.RstToPdf(sphinx=True, stylesheets=self.stylesheets, language=self.__language, breaklevel=self.breaklevel, breakside=self.breakside, fit_mode=self.fitmode, font_path=self.fontpath, inline_footnotes=self.inline_footnotes, highlightlang=self.highlightlang, splittables=self.splittables, style_path=[self.srcdir], basedir=self.srcdir, def_dpi=self.default_dpi, ).createPdf(doctree=self.document, output=sio, compressed=self.compressed) self.output=sio.getvalue()
def convert_pdf(path): import logging from rst2pdf import createpdf parser = createpdf.parse_commandline() options, args = parser.parse_args(None) # loglevel createpdf.log.setLevel(logging.CRITICAL) # infile filename = path.decode('utf-8').encode('mbcs') options.basedir = os.path.dirname(os.path.abspath(filename)) options.infile = open(filename) if filename.endswith('.tmp'): outfile = filename[:-4] + '.pdf' else: outfile = filename + '.pdf' options.outfile = outfile # style options.style = ['ja.style'] #, 'ja.style''styles.style', # stylepath options.stylepath = [os.path.join(MISC_DIR, 'styles')] # fontpath #options.fpath = [os.path.join(os.environ.get('windir', 'C:\\WINDOWS'), 'Fonts')] options.fpath = [os.path.join(MISC_DIR, 'Fonts')] if options.invariant: createpdf.patch_PDFDate() createpdf.patch_digester() createpdf.add_extensions(options) createpdf.RstToPdf(stylesheets=options.style, language=options.language, header=options.header, footer=options.footer, inlinelinks=options.inlinelinks, breaklevel=int(options.breaklevel), baseurl=options.baseurl, fit_mode=options.fit_mode, smarty=str(options.smarty), font_path=options.fpath, style_path=options.stylepath, repeat_table_rows=options.repeattablerows, footnote_backlinks=options.footnote_backlinks, inline_footnotes=options.inline_footnotes, def_dpi=int(options.def_dpi), basedir=options.basedir, show_frame=options.show_frame, splittables=options.splittables, blank_first_page=options.blank_first_page, breakside=options.breakside).createPdf( text=options.infile.read(), source_path=options.infile.name, output=options.outfile, compressed=options.compressed) options.infile.close()