我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用PyPDF2.PdfFileMerger()。
def render_to_response(self, context, **kwargs): context['pdf'] = True pdf_response = HttpResponse(content_type='application/pdf') response = super().render_to_response(context, **kwargs) response.render() try: html = HTML(string=response.content) fileobj = io.BytesIO() html.write_pdf(fileobj) merger = PdfFileMerger() merger.append(fileobj) if self.object.science_case_file: merger.append(self.object.science_case_file.file) if self.object.experimental_design_file: merger.append(self.object.experimental_design_file.file) merger.write(pdf_response) except Exception as exc: error = 'There was an error generating your pdf. {}' messages.error(self.request, error.format(str(exc))) return HttpResponseRedirect(reverse('sciapplications:index')) return pdf_response
def create(self, cr, uid, ids, data, context=None): pool = registry(cr.dbname) merger = PdfFileMerger() outfiles = [] for p in pool.get(self.model).read(cr,uid,ids): outfiles.append(self.newfilename()) sla = self.render(cr,uid,p,data.get('template') or self.template) if self.report_type == 'scribus_sla': os.unlink(outfiles[-1]) return (sla.read(),'sla') command = "xvfb-run -a scribus-ng -ns -g %s -py %s -pa -o %s" % (sla.name,os.path.join(get_module_path('report_scribus'), 'scribus.py'),outfiles[-1]) _logger.info(command) res = os.system(command) sla.close() if not os.path.exists(outfiles[-1]) or os.stat(outfiles[-1]).st_size == 0: raise MissingError('There are something wrong with the template or scribus installation') merger.append(PdfFileReader(file(outfiles[-1], 'rb'))) outfile = tempfile.NamedTemporaryFile(mode='w+b',suffix='.pdf') merger.write(outfile.name) for filename in outfiles: os.unlink(filename) outfile.seek(0) pdf = outfile.read() outfile.close() return (pdf,'pdf')
def main(): all_pdfs = get_all_pdf_files(os.path.expanduser('~lmx/')) if not all_pdfs: raise SystemExit('No pdf file found!') merger = PyPDF2.PdfFileMerger() with open(all_pdfs[0], 'rb') as first_obj: merger.append(first_obj) for pdf in all_pdfs[1:]: with open(pdf, 'rb') as obj: reader = PyPDF2.PdfFileReader(obj) merger.append(fileobj=obj, pages=(1, reader.getNumPages())) with open('merge-pdfs.pdf', 'wb') as f: merger.write(f)
def merge_pdf(request, technology_id): # generate_quality_pdf(request, 1) technology = Technology.objects.get(id=technology_id) operation_groups = technology.operation_groups.all() data = technology_pdf_data(technology_id) merger = PdfFileMerger() file_name = 'technology.pdf' with open(file_name, 'w') as mypdf: mypdf.write(data) merger.append(PdfFileReader(file(file_name, 'rb'))) os.remove(file_name) for operation_group in operation_groups: file_name = 'operation_group_%d.pdf' %(operation_group.id) with open(file_name, 'w') as mypdf: data2= technology_subpicture_pdf_data(operation_group.id) mypdf.write(data2) merger.append(PdfFileReader(file(file_name, 'rb'))) os.remove(file_name) output_name = "%s.pdf" %(technology.code) merger.write(str(output_name)) response = HttpResponse(mimetype='application/pdf') with open(output_name, 'rb') as mypdf: response.write(mypdf.read()) os.remove(output_name) response['Content-Disposition'] = 'attachment; filename=%s' %(output_name) return response
def overlay_hocr_pages(self, dpi, hocr_filenames, orig_pdf_filename): logging.debug("Going to overlay following files onto %s" % orig_pdf_filename) # Sort the hocr_filenames into natural keys! hocr_filenames.sort(key=lambda x: self.natural_keys(x[0] )) logging.debug(hocr_filenames) pdf_dir, pdf_basename = os.path.split(orig_pdf_filename) basename = os.path.splitext(pdf_basename)[0] pdf_filename = os.path.join(pdf_dir, "%s_ocr.pdf" % (basename)) text_pdf_filenames = [] for img_filename, hocr_filename in hocr_filenames: text_pdf_filename = self.overlay_hocr_page(dpi, hocr_filename, img_filename) logging.info("Created temp OCR'ed pdf containing only the text as %s" % (text_pdf_filename)) text_pdf_filenames.append(text_pdf_filename) # Now, concatenate this text_pdfs into one single file. # This is a hack to save memory/running time when we have to do the actual merge with a writer all_text_filename = os.path.join(pdf_dir, "%s_text.pdf" % (basename)) merger = PdfFileMerger() for text_pdf_filename in text_pdf_filenames: merger.append(PdfFileReader(file(text_pdf_filename, 'rb'))) merger.write(all_text_filename) merger.close() del merger writer = PdfFileWriter() orig = open(orig_pdf_filename, 'rb') text_file = open(all_text_filename, 'rb') for orig_pg, text_pg in zip(self.iter_pdf_page(orig), self.iter_pdf_page(text_file)): orig_pg = self._get_merged_single_page(orig_pg, text_pg) writer.addPage(orig_pg) with open(pdf_filename, 'wb') as f: # Flush out this page merge so we can close the text_file writer.write(f) orig.close() text_file.close() # Windows sometimes locks the temp text file for no reason, so we need to retry a few times to delete for fn in text_pdf_filenames: #os.remove(fn) Retry(partial(os.remove, fn), tries=10, pause=3).call_with_retry() os.remove(all_text_filename) logging.info("Created OCR'ed pdf as %s" % (pdf_filename)) return pdf_filename