#!/usr/bin/python import cairo import os import sys # Auxilliary function to draw some crap inside a surface def draw_stuff(ctx, it): for l in range(0, 8): for i in range(0, 8): for j in range(0, 8): ctx.set_source_rgb(i / 10., j / 10., 0.3 * it / 500.) ctx.move_to(i * 72 + j * (72 / 8), l * 72 - it / 50.) ctx.line_to((i + 1) * 72 + j * (72 / 8), (l + 1) * 72 + it / 50.) ctx.set_line_width(4) ctx.stroke() # Draw some crap inside a surface. def draw_my_complex_stuff(ctx, complexity): ctx.set_source_rgb (0.3, 0.3, 0.3) ctx.paint() for i in range(0, complexity): draw_stuff(ctx, i) if len(sys.argv) != 2: print "error: need complexity as argument" sys.exit(1) complexity = int(sys.argv[1]) # First step: draw a single PDF file with the random crap on a (8*72) # x (8*72) PDF surface orig = cairo.PDFSurface("original.pdf", 8 * 72, 8 * 72) ctx = cairo.Context(orig) draw_my_complex_stuff(ctx, complexity) orig.finish() orig_size = os.stat("original.pdf")[6] # Now, redraw the same thing in an anonymous PDF surface orig = cairo.PDFSurface(None, 8 * 72, 8 * 72) ctx = cairo.Context(orig) draw_my_complex_stuff(ctx, complexity) # Create a new surface whose width and height are a quarter of the # original PDF surface. The goal is to display 1/16th of the original # PDF surface into each page of the new surface. new = cairo.PDFSurface("splitted.pdf", 2 * 72, 2 * 72) ctx = cairo.Context(new) for i in range(0, 4): for j in range(0, 4): # Each page is a part of the original PDF surface x_in_orig = - (i * 2 * 72) y_in_orig = - (j * 2 * 72) ctx.set_source_surface(orig, x_in_orig, y_in_orig) ctx.rectangle(0, 0, 2 * 72, 2 * 72) ctx.fill() ctx.show_page() new.finish() splitted_size = os.stat("splitted.pdf")[6] # Show the size of the original file print "Orig surface size once rendered in PDF is : %d bytes" % \ orig_size # Show the size of the 'splitted' file print "Splitted surface size once rendered in PDF is : %d bytes" % \ splitted_size print "Size ratio: %f" % (float(splitted_size) / float(orig_size))