1#!/usr/bin/python
2# $Id: quickreference.py,v 1.18.2.3 2009/03/02 14:14:07 rjs Exp $
3#
4# PDFlib/PDI client: mini imposition demo
5#
6
7from sys import *
8from pdflib_py import *
9
10maxrow = 2
11maxcol = 2
12width = 500.0
13height = 770.0
14
15infile = "reference.pdf"
16searchpath = "../data"
17
18p = PDF_new()
19
20try:
21  try:
22    # This means we must check return values of load_font() etc.
23    PDF_set_parameter(p, "errorpolicy", "return")
24
25    PDF_set_parameter(p, "SearchPath", searchpath)
26
27    if PDF_begin_document(p, "quickreference.pdf", "") == -1:
28        raise PDFlibException("Error: " + PDF_get_errmsg(p))
29
30    PDF_set_info(p, "Creator", "quickreference.py")
31    PDF_set_info(p, "Author", "Thomas Merz")
32    PDF_set_info(p, "Title", "mini imposition demo (Python)")
33
34    manual = PDF_open_pdi_document(p, infile, "")
35    if manual == -1:
36        raise PDFlibException("Error: " + PDF_get_errmsg(p))
37
38    row = 0
39    col = 0
40
41    PDF_set_parameter(p, "topdown", "true")
42
43    endpage = int(PDF_pcos_get_number(p, manual, "length:pages"))
44
45    for pageno in range(1, endpage+1):
46        if row == 0 and col == 0:
47            PDF_begin_page_ext(p, width, height, "")
48            font = PDF_load_font(p, "Helvetica-Bold", "unicode", "")
49            if font == -1:
50                raise PDFlibException("Error: " + PDF_get_errmsg(p))
51            PDF_setfont(p, font, 18)
52            PDF_set_text_pos(p, 24, 24)
53            PDF_show(p, "PDFlib Quick Reference")
54
55        page = PDF_open_pdi_page(p, manual, pageno, "")
56
57        if page == -1:
58            raise PDFlibException("Error: " + PDF_get_errmsg(p))
59
60        optlist = "scale " + repr(1.0/maxrow)
61
62        PDF_fit_pdi_page(p, page,
63                width/maxcol*col, (row + 1) * height/maxrow, optlist)
64        PDF_close_pdi_page(p, page)
65
66        col = col+1
67        if col == maxcol:
68            col = 0
69            row = row+1
70
71        if row == maxrow:
72            row = 0
73            PDF_end_page_ext(p, "")
74
75    # finish the last partial page
76    if row != 0 or col != 0:
77        PDF_end_page_ext(p, "")
78
79    PDF_end_document(p, "")
80    PDF_close_pdi_document(p, manual)
81
82  except PDFlibException:
83    print("PDFlib exception occurred:\n[%d] %s: %s" %
84	((PDF_get_errnum(p)), PDF_get_apiname(p),  PDF_get_errmsg(p)))
85
86  except Exception:
87    print("Exception occurred: %s" % (exc_info()[0]))
88
89finally:
90    PDF_delete(p)
91