By | 14 February 2012

Create a PDF file in PeopleCode using the Query API

Have you ever wanted to create a PDF file in Application Engine or Peoplecode? Well it turns out it is not too hard. The PDF will be somewhat limited but this method can still be very useful in some situations like:

  • You have an application engine that logs items to a table and you want to generate a PDF of the warnings and errors.
  • You want to report on the rows inserted into a processing table
  • You want a basic PDF output based on some SQL

In the example below, we have a query called “CHG_ERROR_LIST” that is defined in query manager. The query does not have any prompts. However, we still have to pass the RunToFile method a dummy PromptRecord object as it is a requirement for the query API.

Note: I have mocked a function called “getWritablePath” that will pass back a directory that is writable to the application server or process scheduler. This is very specific to your installation and where you want the file to go. I have hard coded a value for example purpose so don’t use this as model for your code. You may want to take a look at this article about How to get files to show up in the process monitor.

The resulting PDF for this test code will be in /appl/shared_files/queryToFile.pdf which I can guarantee is not valid for your environment.

function getWritablePath() returns string 

  /* This function should return a writable path in your installation.  */
  return "/appl/shared_files/";


Function queryToFile()
  Local ApiObject &objQry;
  &objQry = %Session.GetQuery();
  If &"CHG_ERROR_LIST", True, True) = 0 Then
   Local string &path;
   &path = getWritablePath() | "queryTofile.pdf";
   If &objQry.RunToFile(&objQry.PROMPTRECORD, &path, %Query_PDF, 0) = 0 Then
      /* A zero return means success which is counter-intuitive */

Additional Reading

PeopleCode Query API PeopleBooks

Article Categories

Do you want to become a CI Expert?

If you want to learn more about developing using Component Interface then check out our CI Training Video.