October 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

A PDF Solution for All Programming Platforms

  • July 12, 2006
  • By Victor Volkman
  • Send Email »
  • More Articles »

Now you are ready to use a PDF Import function ("pdi") to import your template file. Note that this code won't link on PDFLite as this is a PDFlib function:

51       blockcontainer = p.open_pdi(infile, "", 0);
52       if (blockcontainer == -1) {
53       cerr << "Error: " << p.get_errmsg() << endl;
54          return(2);
55       }
56

Your stuff belongs on the first page, so fetch that page and make it active for writing:

57       page = p.open_pdi_page(blockcontainer, 1, "");
58       if (page == -1) {
59       cerr << "Error: " << p.get_errmsg() << endl;
60          return(2);
61       }
62
63       p.begin_page_ext(20, 20, "");    // dummy page size
64
65       // This will adjust the page size to the block container's size.
66       p.fit_pdi_page(page, 0, 0, "adjustpage");
67

Now, you use the block filling functions, part of the PDFlib Personalization Server (PPS) that allows you to handle variable data blocks of type Text, Image, and PDF. Each data block is addressed by name (for example, "business.telephone.fax"). In this simplistic scheme, you have all of this just sitting in a static structure (go back to Line 23 for details):

68       // Fill all text blocks with dynamic data
69       for (i = 0; i < (int) BLOCKCOUNT; i++) {
70          if (p.fill_textblock(page, data[i].name, data[i].value,
71          "embedding encoding=host") == -1) {
72          cerr << "Error: " << p.get_errmsg() << endl;
73          }
74       }
75

Because you are now fairly deep into the document, you must remember to finish the page, close the imported document page, finalize your own document, and then close the imported file handle:

76       p.end_page_ext("");
77    p.close_pdi_page(page);
78
79       p.end_document("");
80    p.close_pdi(blockcontainer);
81    }
82
83    catch (PDFlib::Exception &ex) {
84       cerr << "PDFlib exception occurred in businesscard sample: "
         << endl;
85       cerr << "[" << ex.get_errnum() << "] " << ex.get_apiname()
86       << ": " << ex.get_errmsg() << endl;
87    return 99;
88    }
89
90    return 0;
91 }

Now, you can open or print your output file (businesscard.pdf) in Adobe Acrobat, e-mail it to a customer, burn it to a CD, or upload it to an online printer.

For Further Reading

Beginning PDF Programming with PHP and PDFlib
by Ron Goff
Foreword by Thomas Merz

ISBN 0973589841

Although written with PHP developers in mind, this book covers all the basics of the PDFlib API, which is essentially platform-independent. I highly recommend it for anyone who is starting off with PDFlib development or curious about what it can really do.

Meet the Whole Family

You truly have only scratched the surface of what PDFlib offers. The programming API is also part of a larger product family:

  • PDFlib contains all functions required to create PDF output containing text, vector graphics and images, plus hypertext elements.
  • PDFlib+PDI includes all PDFlib functions, plus the PDF Import Library (PDI) for including pages from existing PDF documents in the generated output.
  • PDFlib Personalization Server (PPS) includes PDFlib+PDI, plus additional functions for automatically filling PDFlib blocks. Blocks are placeholders on the page that can be filled with text, images, or PDF pages. They can be created interactively with the PDFlib Block Plugin for Adobe Acrobat (Mac or Windows) and will be filled automatically with PPS. The plugin is included in PPS.

About the Author

Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for Washtenaw Community College CIS department. Volkman is the editor of numerous books, including C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries; just drop an e-mail to sysop@HAL9K.com.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel