def generate_pdf(self, raw=None, FORCE=False): """Creat pdf and return path to pdf file. Use given rst text or use raw_report. if FORCE is true, create a new pdf even if stored one exists """ # get the temp names tmp_rstfilename = tempfile.mkstemp(suffix='.rst')[1] #tmp_pdffilename = tempfile.mkstemp(suffix='.pdf')[1] pdffilename = self.get_pdffilename() # on Windows, pdf does not get rendered if the pdf is created in the # system temp folder (? reason) # if OS == 'windows': # tmp_folder = os.path.join(os.path.dirname(self.template_file), 'tmp') # tmp_pdffilename = os.path.join(tmp_folder, 'report.pdf') # if not os.path.exists(tmp_folder): # os.mkdir(tmp_folder) # If previously rendered pdf is available, display it if os.path.exists(pdffilename) and not FORCE: return pdffilename # previously rendered pdf does not exist, create new one if not raw: raw = self.raw_report # convert image filenames to full path (will vary per system) # templates (and generated rst) will have image paths specified as # {{image_folder/logo.png}} def replace_with_full_path(imagefilename_match): fullpath = os.path.join(self.image_folder, imagefilename_match.group('imagefilename')) return fullpath raw = re.sub('{{(?P<imagefilename>[\w\s\d\.]*)}}', replace_with_full_path, raw) # write the raw_report as a file with open(tmp_rstfilename, 'w') as fi: fi.write(raw) # invoke rst2pdf if self.stylefile: cmd = ['-s', self.stylefile, tmp_rstfilename, '-o', pdffilename] else: cmd = [tmp_rstfilename, '-o', pdffilename] createpdf.main(cmd) return pdffilename
def generate_pdf(self, raw=None, FORCE=False): """Creat pdf and return path to pdf file. Use given rst text or use raw_report. if FORCE is true, create a new pdf even if stored one exists """ # get the temp names tmp_rstfilename = tempfile.mkstemp(suffix='.rst')[1] #tmp_pdffilename = tempfile.mkstemp(suffix='.pdf')[1] pdffilename = self.get_pdffilename() # on Windows, pdf does not get rendered if the pdf is created in the # system temp folder (? reason) # if OS == 'windows': # tmp_folder = os.path.join(os.path.dirname(self.template_file), 'tmp') # tmp_pdffilename = os.path.join(tmp_folder, 'report.pdf') # if not os.path.exists(tmp_folder): # os.mkdir(tmp_folder) # If previously rendered pdf is available, display it if os.path.exists(pdffilename) and not FORCE: return pdffilename # previously rendered pdf does not exist, create new one if not raw: raw = self.raw_report # convert image filenames to full path (will vary per system) # templates (and generated rst) will have image paths specified as # {{image_folder/logo.png}} def replace_with_full_path(imagefilename_match): fullpath = os.path.join(self.image_folder, imagefilename_match.group('imagefilename')) return fullpath raw = re.sub('{{(?P<imagefilename>[\w\s\d\.]*)}}', replace_with_full_path, raw) # write the raw_report as a file with open(tmp_rstfilename,'w') as fi: fi.write(raw) # invoke rst2pdf if self.stylefile: cmd = ['-s', self.stylefile, tmp_rstfilename, '-o', pdffilename] else: cmd = [tmp_rstfilename, '-o', pdffilename] createpdf.main(cmd) return pdffilename
def rst2pdf_nops(): """Use rst2pdf python package to directly produce pdf document from rst""" from rst2pdf import createpdf createpdf.main(['rst.rst'])
def rstToPdf(directory): import rst2pdf.createpdf as createpdf for filePath, pdfPath in iterFiles(directory, '.pdf'): print filePath print 'to pdf: ', pdfPath createpdf.main([filePath, '-o', pdfPath])