Purpose
Generate a PDF document

Solution (via MS Word automation)

  1. Collect all necessary data in GuiXT variables
  2. Generate an HTML or RTF file, using the CopyText fromTemplate= command
  3. Use a JavaScript function to have the file converted into PDF format via MS Word

Example
We add a button "Order confirmation PDF" in the initial screen of VA03:



In VA03 the document looks as follows::

When the user presses our new button the following PDF is generated and displayed:

We start the implementation by creating a template file for the order confirmation document in MS Word.

  •  For each variable part of the document, we use an expression &[varname] where varname is the name of a GuiXT variable.

  • For tables we create only one row and use the notation &[varname###] in the table cells. GuiXT will replace ### by 1,2,3,... and generate the correct number of table rows.

  • We save the template document in MS Word as RTF file.

Important hint: The expressions &V[varname] can be in any format you like (font, size, color,...) but no format changes are allowed within the expression.
To assure this, we suggest you first copy the expression &[varname] into a simple editor such as notepad.exe and then copy it from there into the template document
.


For the example above we use the following template file:


Here is a list of the GuiXT variables used in the template:

Order number
&[ordernumber]

Customer address
&[addr.name]
&[addr.street]
&[addr.city]
&[addr.postl_code]

Order header
&[oh.doc_date]
&[oh.purch_no]
&[oh.currency]

Order items
&[oi.no.###]
&[oi.material.###]
&[oi.text.###]
&[oi.qty.###]
&[oi.unit.###]
&[oi.value.###]

Total price
&[total_value]

The InputScript reads the order data via BAPIs, then reads the RTF template into a GuiXT text variable, having all template variables replaced by GuiXT, and saves the result as a temporary RTF file. Finally a JavaScript funtion is called up, and it converts the RTF file into a PDF, using MS Word automation.

InputScript

// current order number in VA03 screen
Set V[ordernumber] "&F[Order]" 

// order number specified?
if
not V[ordernumber]
  Message "E: Please enter an order number" -statusline
  Return
endif

// Part 1: Read data into GuiXT variables

// strutures/tables for BAPI calls and RTF generation
CreateStructure V[oh] doc_date purch_no sold_to sales_org currency
CreateTable V[oi] no material text qty unit value currency
CreateStructure V[addr] name city street postl_code
CreateStructure V[item] itm_number material short_text _
                            cum_cf_qty sales_unit net_value

CreateTable V[items] include=V[item]

// Try to read order
Call BAPI_SALESORDER_GETDETAILBOS -try _
 
export.SALESDOCUMENT="ordernumber" _
 
import.ORDERHEADER="oh"
 
import.ORDERITEMS="items"

// order number valid?
if not V[oh.sold_to]
  Message "E: Please enter a valid order number" -statusline
  Return
endif

Set V[k] 1
Set V[total_value] 0
label next_item
ReadRow V[item] table=V[items] index=&V[k]
if Q[ok]
 
Set V[oi.no.&V[k]] "&V[item.itm_number]" + 0   // to remove leading zeros
  Set V[oi.material.&V[k]] "&V[item.material]"
  Set V[oi.text.&V[k]] "&V[item.short_text]"

  // read number of decimal places for given unit
  Call RSW_UNIT_DECIMALS_GET  cache="session" _
   
export.I_UNIT="item.sales_unit" _
   
import.E_decimals="mydecimals"

 
// format quantity accordung to unit
  Set V[oi.qty.&V[k]] "&V[item.cum_cf_qty]" + 0 decimals="&V[mydecimals]" _
           decimalSeparator=
"," groupSeparator="."
  Set V[oi.unit.&V[k]] "&V[item.sales_unit]"

 
// net value
  Set V[oi.value.&V[k]] "&V[item.net_value]" + 0 _
         
decimals=2 decimalSeparator="," groupSeparator="."

  // add to total value
  Set V[total_value]  "&V[total_value]" +  "&V[oi.value.&V[k]]"  _
        
decimals=2 decimalSeparator="," groupSeparator="."

   // increase item number
  Set V[k] &V[k] + 1
 
goto next_item
endif

// Read customer address
Call BAPI_CUSTOMER_GETDETAIL  _
  export.CUSTOMERNO="oh.sold_to" _
 
export.PI_SALESORG="oh.sales_org" _ 
 
import.PE_ADDRESS="addr" 


// Part 2: Generate PDF

// temporary file names
Set V[rtffile] "&%[TEMP]\guixt.orderconfirmation.&V[today_y-m-d hms].rtf"
Set V[pdffile] "&%[TEMP]\guixt.orderconfirmation.&V[today_y-m-d hms].pdf"

// copy template RTF into a text variable, replace all variables
CopyText fromTemplate="E:\wordjs\orderconfirmation.template.rtf" toText="temp"

// copy to RTF file
CopyText fromText="temp" toFile="&V[rtffile]"

// convert RTF to PDF
CallJS  convertfile "&V[rtffile]" "&V[pdffile]"

// open PDF
Start "&V[pdffile]"

Return

JavaScript "convertfile"

function convertfile(inputfile, outputfile) {

     var msword = guixt.CreateObject("Word.Application");
     msword.Visible = false;
     msword.DisplayAlerts = false;

     var doc = msword.Documents.Add();
     msword.Selection.InsertFile(inputfile);

     var extension = outputfile.split('.').pop();
     extension = extension.toLowerCase();

     var wordformat = 0;

    switch (extension)
   {
       
case "pdf": wordformat = 17; break;
        case "doc": wordformat = 0; break;
        case "htm": case "html": wordformat = 8; break;
        case "rtf": wordformat = 6; break;
        case "odt": wordformat = 23; break;
        case "docx": default: wordformat = 16; break;
    }

    // save
    doc.SaveAs(outputfile, wordformat);

    doc.Close(0);
    msword.Quit();

}

Project files "orderconfirmation.zip"


Components
InputAssistant + Controls