Skip to content

Vector Image Support

Jorj X. McKie edited this page Jan 6, 2018 · 6 revisions

Via another supplement for version 1.12.1, vector images are now supported to the following extent.

Display

To our knowledge, PDF does not directly support vector images like SVG. It is however possible to display arbitrary content provided in PDF operator syntax (see APPENDIX A, Operator Summary of the Adobe manual).

As a first step, we have created a page method showPDFpage which places a vector image of a page from another PDF in a specified rectangle.

This method can for instance be used to create double-paged or "4-up" versions of existing PDFs. Here is a script that places 4 pages of the input on each output page:

from __future__ import print_function
import fitz, time
doc = fitz.open()
src = fitz.open(infile)

r = fitz.Rect(0, 0, 595, 842)          # A4 portrait output page format: adjust!

# define the 4 rectangles per page
r1 = fitz.Rect(0, 0, r.width/2, r.height/2)
r2 = r1 + (r1.width, 0, r1.width, 0)
r3 = r1 + (0, r1.height, 0, r1.height)
r4 = fitz.Rect(r1.br, r.br)

# put them in an array
r_tab = (r1,r2,r3,r4)

t0 = time.clock()

# copy input to output
for spage in src:
    if spage.number % 4 == 0:
        page = doc.newPage(-1, width = r.width, height = r.height)
    # put input page in the correct rectangle of output page
    page.showPDFpage(r_tab[spage.number % 4], src, spage.number,
                     keep_proportions = True)

t1 = time.clock()

# save new file using garbage collection and compression
doc.save("4up-" + infile, garbage = 4, deflate = True)

# log output
t2 = time.clock()
print("processed %i pages of file '%s'" % (len(src), src.name))
print("showPDFpage time: %g" % (t1-t0))
print("save time: %g" % (t2-t1))

Another usage may be displaying the same thumbnail (e.g. a company logo stored in a 1-pager PDF) on every page of a brochure. This has a similar effect like insertImage but maintains display precision across zooming.

Typical vector images come in SVG format. These can be coverted to PDF by a number of tools, like Apache Batik (Java) or the Python package svglib.

SVG Output

Page method getSVGimage creates an SVG image of the page returned as a unicode string. This string can be save as an SVG-file.

Clone this wiki locally