1#!/usr/bin/env ruby 2# $Id: starter_basic.rb,v 1.3.2.5 2008/02/28 14:49:59 rjs Exp $ 3# 4# Basic starter: 5# Create some simple text, vector graphics and image output 6# 7# required software: PDFlib Lite/PDFlib/PDFlib+PDI/PPS 7 8# required data: none 9# 10 11require 'PDFlib' 12 13# This is where the data files are. Adjust as necessary. 14searchpath = "../data" 15 16imagefile = "nesrin.jpg" 17 18begin 19 p = PDFlib.new 20 21 # This means we must check return values of load_font() etc. 22 p.set_parameter("errorpolicy", "return") 23 24 p.set_parameter("SearchPath", searchpath) 25 26 # we use "utf8" as textformat, this allows to use unicode encoding 27 p.set_parameter("textformat", "utf8"); 28 29 if (p.begin_document("starter_basic.pdf", "") == -1) 30 raise "Error: " + p.get_errmsg() 31 end 32 33 p.set_info("Creator", "PDFlib starter sample") 34 p.set_info("Title", "starter_basic") 35 36 # We load the image before the first page, and use it 37 # on all pages 38 # 39 image = p.load_image("auto", imagefile, "") 40 41 if (image == -1) 42 raise "Error: " + p.get_errmsg() 43 end 44 45 # Page 1 46 p.begin_page_ext(595, 842, "") 47 48 # For PDFlib Lite: change "unicode" to "winansi" 49 font = p.load_font("Helvetica-Bold", "unicode", "") 50 if font == -1 51 raise "Error: " + p.get_errmsg + "\n" 52 end 53 54 p.setfont(font, 24) 55 56 p.set_text_pos(50, 700) 57 p.show("Hello world!") 58 59 p.fit_image(image, 0.0, 0.0, "scale=0.25") 60 61 p.end_page_ext("") 62 63 # Page 2 64 p.begin_page_ext(595, 842, "") 65 66 # red rectangle 67 p.setcolor("fill", "rgb", 1.0, 0.0, 0.0, 0.0) 68 p.rect(200, 200, 250, 150) 69 p.fill() 70 71 # blue circle 72 p.setcolor("fill", "rgb", 0.0, 0.0, 1.0, 0.0) 73 p.arc(400, 600, 100, 0, 360) 74 p.fill() 75 76 # thick gray line 77 p.setcolor("stroke", "gray", 0.5, 0.0, 0.0, 0.0) 78 p.setlinewidth(10) 79 p.moveto(100, 500) 80 p.lineto(300, 700) 81 p.stroke() 82 83 # Using the same image handle means the data will be copied 84 # to the PDF only once, which saves space. 85 # 86 p.fit_image(image, 150.0, 25.0, "scale=0.25") 87 p.end_page_ext("") 88 89 # Page 3 90 p.begin_page_ext(595, 842, "") 91 92 # Fit the image to a box of predefined size (without distortion) 93 optlist = "boxsize={400 400} position={center} fitmethod=meet" 94 95 p.fit_image(image, 100, 200, optlist) 96 97 p.end_page_ext("") 98 99 p.close_image(image) 100 p.end_document("") 101 102rescue PDFlibException => pe 103 print "PDFlib exception occurred:\n" 104 print "[" + pe.get_errnum.to_s + "] " + pe.get_apiname + \ 105 ": " + pe.get_errmsg + "\n" 106rescue Exception => e 107 print e.backtrace.join("\n") + "\n" + e.to_s 108ensure 109 p.delete() if p 110end 111# vim: sw=2 112