def __init__(self, pdf): self.list_data = list( ) # to store the states of ordered/unordered list # TODO: put this in render() Directory containing markdown (and images) self.indir = None self.pdf = pdf self.doc = fitz.open() self.toc = [] self.currentPage = None global width, height width, height = fitz.PaperSize( properties.paperSize) # choose paper format
def pdftopic(self, pdffile, rate=50): doc = fitz.open(pdffile) width, height = fitz.PaperSize("a4") for pg in range(doc.pageCount): page = doc[pg] zoom = int(rate) rotate = int(0) trans = fitz.Matrix(zoom / 100.0, zoom / 100.0).preRotate(rotate) pm = page.getPixmap(matrix=trans, alpha=False) pic = self.picpath + str(pg + 1) + ".jpg" pm.writePNG(pic) doc.close()
assert startyear.isdigit(), "year must be positive numeric" startyear = int(startyear) assert startyear > 0, "year must be positive numeric" # We use a nicer mono-spaced font than the PDF builtin 'Courier'. # If you do not know one, set ffile to None and fname to 'Courier' ffile = "c:/windows/fonts/dejavusansmono.ttf" fname = "F0" doc = fitz.open() cal = calendar.LocaleTextCalendar(locale="es") # use your locale #cal = calendar.TextCalendar() # or stick with English w, h = fitz.PaperSize("a4-l") # get sizes for A4 landscape paper txt = cal.formatyear(startyear, m=4) doc.insertPage(-1, txt, fontsize=12, fontname=fname, fontfile=ffile, width=w, height=h) txt = cal.formatyear(startyear + 1, m=4) doc.insertPage(-1, txt, fontsize=12, fontname=fname,
The only purpose of this ugly picture is to show that creating it with PyMuPDF definitely is at least as easy and straightforward ... """ green, pink, brown = getColor("green"), getColor("pink"), getColor("brown") clist = (pink, green, brown) # make a list of these colors top = 72 # some convenience constants left = right = top / 2 nrects = 14 fname = "Helvetica-Bold" # fontname doc = fitz.open() # empty new document cw = doc._getCharWidths(fname) # get glyph widths of the font text = "SPUMONI" # the ominous text textl = sum([cw[ord(c)] for c in text]) # get total glyph width of text width, height = fitz.PaperSize("letter") page = doc.newPage(-1, width = width, height = height) # insert new page rwidth = (width - 2*left) / nrects # rect width, leaves borders of 36 px fsize = (width - 2*left) / textl # optimum fontsize rheight = 400 # rect height for i in range(nrects-2): # draw colored rectangles (last 2 stay white) r = fitz.Rect(left + i*rwidth, top, left + (i+1)*rwidth, top + rheight) page.drawRect(r, color = clist[i % 3], fill = clist[i % 3]) # draw outer black border page.drawRect(fitz.Rect(left, top, width - right, top + rheight)) # insert the big text in white letters, a little above rectangle's middle line page.insertText(fitz.Point(left, top + (rheight + fsize*0.7)/2), text, fontsize=fsize, fontname = fname, color = getColor("white")) # now the wafer cone with its 3 defining points points = (fitz.Point(width/2, top + rheight), # center of bottom line
(3) This should run very fast: needed less than 25 sec on a Python 3.6 64bit, Windows 10, AMD 4.0 GHz for the 1'310 pages of the Adobe manual. Without save-options "garbage" and "deflate" this goes below 4 seconds, but results in a bigger file. Dependencies ------------- PyMuPDF 1.12.1 or later ''' from __future__ import print_function import fitz, sys infile = sys.argv[1] src = fitz.open(infile) doc = fitz.open() # empty output PDF width, height = fitz.PaperSize("a4") # A4 portrait output page format r = fitz.Rect(0, 0, width, height) # define the 4 rectangles per page r1 = r * 0.5 # top left rect r2 = r1 + (r1.width, 0, r1.width, 0) # top right r3 = r1 + (0, r1.height, 0, r1.height) # bottom left r4 = fitz.Rect(r1.br, r.br) # bottom right # put them in a list r_tab = [r1, r2, r3, r4] # now copy input pages to output for spage in src: if spage.number % 4 == 0: # create new output page page = doc.newPage(-1, width=width, height=height)
for f in files: if f.endswith('.pdf'): input_file = join(root, f) output_file = join('D:\\py\\new_CR\\exception', f) barcode_mark = 'D:\\py\\title.png' doc = fitz.open(input_file) page = doc[0] rect_mark = fitz.Rect(0, 0, 1024, 50) pix_1 = fitz.Pixmap(barcode_mark) page.insertImage(rect_mark, pixmap=pix_1, overlay=True) doc.save(output_file) for root, dirs, files in walk('D:\\py\\new_CR\\exception'): for f in files: doc = fitz.open(join(root, f)) width, height = fitz.PaperSize('a4') totaling = doc.pageCount for pg in range(totaling): page = doc[pg] zoom = int(100) rotate = int(0) trans = fitz.Matrix(zoom / 60, zoom / 60).preRotate(rotate) pm = page.getPixmap(matrix=trans, alpha=False) lurl = 'D:\\py\\new_CR\\{}.jpg'.format(str(f)[0:12]) pm.writePNG(lurl) doc.close() for root, dirs, files in walk('D:\\py\\new_CR\\exception'): for f in files: if '.jpg' in f: doc_pdf = fitz.open()
if path[-4:] == ".pdf": pdffiles.append(path) # Try to sort after 3rd part in filename try: idx = sortFiles([os.path.basename(s) for s in pdffiles], args.sep, args.nsort) pdffiles = [pdffiles[i] for i in idx] print([os.path.basename(s) for s in pdffiles]) except Exception as e: print("Something went wrong while sorting filenames, continuing without:") print(e) doc = fitz.open() pagesize="A4" width, height = fitz.PaperSize(pagesize) for path in pdffiles: print("adding {}".format(path)) incremental = doc.pageCount > 0 # need an initial document for incremental save # flag for printing two landscape pages onto portrait isFirstLandscape = True src = fitz.open(path) for n in range(src.pageCount): if pageOrientation(src[n]) == "portrait": pos = 0 page = doc.newPage(-1, width = width, height = height)
rc = False if str is bytes: imgdir = sys.argv[1] # where my files are else: rc, imgdir = psg.GetPathBox("Make a PDF from Embedded Files", "Enter file directory:") if not imgdir: raise SystemExit() t0 = mytime() # set start timer doc = fitz.open() width, height = fitz.PaperSize("a4") rect = fitz.Rect(0, 0, width, height) + (36, 36, -36, -36) imglist = os.listdir(imgdir) imgcount = len(imglist) for i, f in enumerate(imglist): path = os.path.join(imgdir, f) if not os.path.isfile(path): print("skipping non-file '%s'!" % f) continue if str is not bytes: psg.EasyProgressMeter("Embedding Files", i + 1, imgcount, "dir: " + imgdir, "file: " + f) else: print("embedding file '%s', (%i / %i)" % (f, i + 1, imgcount))
Formula of lines per page (nlines) is also used by the 'insertPage' method. ------------------------------------------------------------------------------ """ def pixlen(text, widthlist, fontsize): """Calculate the length of text in pixels, given a list of (font-specific) glyph widths and the fontsize. Parameter 'widthlist' should have been created by 'doc._getCharWidths()'.""" pl = 0.0 for t in text: pl += widthlist[ord(t)] return pl * fontsize width, height = fitz.PaperSize("a4") # choose paper format fontsz = 10 # choose font size of text lineheight = fontsz * 1.2 # line height is 20% larger # the above gives the following lines per page: nlines = int((height - 108.0) / lineheight) # choose a nice mono-spaced font of the system, instead of 'Courier'. # To use a standard PDF base14 font, e.g. set font='Courier' and ffile=None ffile = "C:/windows/fonts/consola.ttf" # font file font = "F0" # fontname sourcefile = open(ifn) # we are going to convert this file line_ctr = 0 # page line counter total_ctr = 0 # total line counter out_ctr = 0 # count output lines out_buf = "" # text of one page
else: mytime = time.clock rc = False if str is bytes: imgdir = sys.argv[1] # where my files are else: rc, imgdir = psg.GetPathBox("Make a PDF from Attached Files", "Enter file directory:") if not imgdir: raise SystemExit() t0 = mytime() # set start timer width, height = fitz.PaperSize("a6-l") # get paper format doc = fitz.open() # open empty PDF page = doc.newPage(width = width, # make new page height = height) # define sub rect to receive text and annotation symbols rect = fitz.Rect(0, 0, width, height) + (36, 36, -36, -36) imglist = os.listdir(imgdir) # directory listing imgcount = len(imglist) # number of files # calculate number of pages we will create per_page = ((width - 72) // 25) * ((height - 36 - 56) // 35) pages = int(round(imgcount / per_page + 0.5))
import fitz import calendar import sys assert len(sys.argv) == 2, '"need start year as the one and only parameter"' startyear = sys.argv[1] assert startyear.isdigit(), "year must be positive numeric" startyear = int(startyear) assert startyear > 0, "year must be positive numeric" doc = fitz.open() cal = calendar.LocaleTextCalendar(locale="en") w, h = fitz.PaperSize('a4-l') txt = cal.formatyear(startyear, m=4) doc.insertPage(-1, txt, fontsize=12, fontname="Courier", width=w, height=h) txt = cal.formatyear(startyear + 1, m=4) doc.insertPage(-1, txt, fontsize=12, fontname='Courier-Oblique', width=w, height=h) txt = cal.formatyear(startyear + 2, m=4) doc.insertPage(-1, txt, fontsize=12, fontname="Courier-Bold", width=w,