def create_pdf(self, path, name, sum_real_cpu_time, if_graphs): """Creates pdf-file""" from fpdf import FPDF def h1(text): """Makes a header, text-size 22""" pdf.set_font("Courier", 'B', size=22) pdf.cell(190, 12, txt=text, ln=1, align="C") pdf.set_font("Courier", size=11) pdf = FPDF() pdf.add_page() h1("Analysis") def print_bench_res(kk, link): """Prints sums of benchmarks' t and cpu""" pdf.cell(10) pdf.cell(100, 5, txt='{}'.format(self.presenter.benchmark_names[kk]), ln=1, link=link) pdf.cell(20) pdf.cell(100, 5, txt='t = {:+7.4f}; CPU = {:+7.4f}'.format( sum_real_cpu_time[kk][0], sum_real_cpu_time[kk][1]), ln=1, link=link) pdf.cell(0, 2, ln=1) pdf.set_text_color(r=80, g=0, b=0) pdf.cell(100, 10, txt="Benchmarks with worsened performance:", ln=1) pdf.set_text_color(r=0) links = [] for k in range(len(self.presenter.benchmark_names)): links += [pdf.add_link()] if sum_real_cpu_time[k][0] >= 0.05 or sum_real_cpu_time[k][ 1] >= 0.05: print_bench_res(k, links[k]) pdf.set_text_color(r=0, g=80, b=0) pdf.cell(100, 10, txt="Benchmarks with improved performance:", ln=1) pdf.set_text_color(r=0) for k in range(len(self.presenter.benchmark_names)): if sum_real_cpu_time[k][0] <= -0.05 or sum_real_cpu_time[k][ 1] <= -0.05: print_bench_res(k, links[k]) # Если вывод был full - добавляем графики if if_graphs: pdf.add_page() h1("Graphs") for i in range(len(self.presenter.benchmark_names)): pdf.image('{}{}{}{}'.format(path, '/fig_', i, '.png'), w=160) pdf.set_link(links[i]) pdf.cell(0, 5, ln=1) pdf.output(path + name)
def test_link_with_zoom_and_shift(tmp_path): pdf = FPDF() pdf.set_font("helvetica", size=24) pdf.add_page() link = pdf.add_link() pdf.set_link(link, page=2, x=pdf.epw / 4, y=pdf.epw / 3, zoom=4) pdf.set_xy(30, 50) pdf.cell( w=140, h=10, txt="Link to 2nd page zoomed & shifted", border=1, align="C", link=link, ) pdf.add_page() pdf.multi_cell( pdf.epw, txt="Lorem ipsum dolor sit amet, consectetur adipiscing elit," " sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." " Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." " Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur." " Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", ) # Drawing Adobe Reader viewport after clicking on the link, # with the right panel open. The initial zoom level does not matter. pdf.set_draw_color(r=255, g=0, b=0) pdf.rect(x=pdf.epw / 4, y=pdf.epw / 3, w=53.5, h=31) assert_pdf_equal(pdf, HERE / "link_with_zoom_and_shift.pdf", tmp_path)
def create_PDF_file(sourcedir, dest, name): print('Creating {}/{}'.format(dest, name)) pdf = FPDF(orientation="P",unit="mm",format="A4") pdf.set_compression(True) pdf.add_page() pdf.add_font('FreeMono', '', 'font/FreeMono.ttf', uni=True) pdf.set_margins(0,0,0) files = os.listdir(sourcedir) # create destination directory if not present if not os.path.exists(dest): os.makedirs(dest) # write index if 'index.txt' in files: print('Index found') pdf.set_font('FreeMono',size=12) with open('{}/index.txt'.format(sourcedir),'r', encoding='cp1250') as index: entries = index.readlines() for entry in entries: pagenum = int(entry[-4:]) link = pdf.add_link() pdf.set_link(link, page=pagenum +1) pdf.set_x(0) # print('{} {}'.format(entry[:-6],pagenum)) pdf.write(5, '{}...........{}\n'.format(entry[:-6],pagenum), link) print('Index built') # remove all except .jpg files = list(filter(lambda x: x.endswith('.jpg'), files)) # sort by numbers files.sort(key=lambda x: int(x[-8:-4])) print('Adding {} images'.format(len(files))) for imag in files: pdf.image(name='{}/{}'.format(sourcedir, imag), type='JPG', w=210, h=297) print('Writing pdf file...') pdf.output('{}/{}'.format(dest, name), 'F') pdf.close() print('Done\n')
def main(): pdf = FPDF() pdf.add_font("roboto", fname="./RobotoMono-Regular.ttf", uni=True) pdf.add_font("roboto", style='I', fname='./RobotoMono-Italic.ttf', uni=True) pdf.add_page() header = Header() header.place(pdf) subItem = SubItem(link=1) subItem.place(pdf) to_page_2 = pdf.add_link() paragraph = Paragraph() paragraph.place(pdf) image = Image("./img.jpg") image.place(pdf, 100, 50, align="C", caption="Fig 1.1: A fox") pdf.add_page() paragraph.place(pdf) pdf.set_link(to_page_2, page=2) pdf.output("test.pdf","F")