1. Olan Knight
  2. PowerBuilder
  3. Friday, 2 September 2022 22:09 PM UTC

UPDATE:  Tuesday, 06-SEP-2022.  I have attached the two function I used to make this work. (merge_two_pdf_files.txt)

Arg! Text format attachments are not allowed. (Why not?)
Fine.
The code is in the last entry of this thread.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


UPDATE:  Friday, 02-SEP-2022

   The problem was that the FileClose() command returns  >>> BEFORE <<< the file that is being created has actually closed! We are creating PDFs, which take far more time to completely close than a TXT file (for example).  The code is executed so fast that when the PDFtk command is executed, that file that we FileClosed, which is included in the command line to be processed by PDFtk, does not actually yet exist.

Hot news flash:  PDFtk requires files that actually exist in order to process those files.  :/

   The problem was correct by adding code to ensure that the file actually exists before invoking the PDFTk command line.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


PowerBuilder 2019R3, build 2703
Window 10, 64 bit platform
PDFtk_Pro


Chris, I used your suggestion to let PDFtk Pro do our PDF merges.

I purchased PDFtk_Pro today and installed it. No errors.
I updated my code to generate the following command line:

     pdftk "Q:\CABSv4\PDF_Notices\018H - PDF Notice #001.pdf"  "C:\Personal\Test\018H-018HD0432E-D0432E220902_temp.pdf" cat output "C:\Personal\Test\018H-018HD0432E-D0432E220902.pdf" 

This command works perfectly when I run it in a CMD session/cmd prompt window, but fails when I execute RUN (ls_cmd) in the code.


WHAT am I doing wrong in the code?

~~~~~~~~~~~~~~~~~~~~~~~

Works in the CMD prompt:

Resulting file verified:


Fails in the code:

Olan Knight Accepted Answer Pending Moderation
  1. Tuesday, 6 September 2022 15:17 PM UTC
  2. PowerBuilder
  3. # 1

//*********************************************************************
// Object        :    n_cst_pdf
// Function    :  of_merge_two_pdf_files
//
// Ancestor        :    Extended
// Access        :    Public
// Arguments    :    value   string   as_first_pdf_file
//                        value   string   as_second_pdf_file
//                        value   string   as_result_filename
//
// Returns        :    String              Error msg; Empty string = no error
// Throws        :    None
//
// Description    :    Merge the PDF files as specified, with the resulting
//                        merged file to be named <as_result_filename>.
//
//********************************************************************
// Revision History
//
// Developer    Date              Version      Description
// ---------    -----------      ---------      -------------------------------
// O Knight    02-SEP-2022   4.0.5.240   #5440515:  Initial install.
//
//********************************************************************
// COPYRIGHT © 2022 CSG SYSTEMS INTERNATIONAL, INC. AND/OR ITS
// AFFILIATES (“CSG”). ALL RIGHTS RESERVED.
//********************************************************************
boolean        lb_exists
long            ll_rc, ll_pos
string        ls_err, ls_file [3], ls_path [3], ls_cmd, ls_msg



// Validate the input files
// ~~~~~~~~~~~~~~~~~~~~~~~~
ls_err = this.FUNCTION of_wait_for_file_to_be_created (as_first_pdf_file)
IF (ls_err <> "") THEN
    ls_err = "The first PDF file to be merged does not exist, of the code does not " + &
                "have access to the file. ~r~n Filename:  " + as_first_pdf_file
    GOTO Exit_Function
END IF

ls_err = this.FUNCTION of_wait_for_file_to_be_created (as_second_pdf_file)
IF (ls_err <> "") THEN
    ls_err = "The second PDF file to be merged does not exist, of the code does not " + &
                "have access to the file. ~r~n Filename:  " + as_second_pdf_file
    GOTO Exit_Function
END IF

// Ensure the output/result file does NOT exist
lb_exists = FileExists (as_result_filename)
IF (lb_exists) THEN
    
    lb_exists = FileDelete (as_result_filename)
    
END IF


// Separate the path and file names (Used in tracking)
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// Split the first filename
ll_pos = LASTPOS (as_first_pdf_file, "\")
IF (ll_pos > 0) THEN
    
    ls_file [1] = MID (as_first_pdf_file, (ll_pos + 1), 9999)
    ls_path [1] = MID (as_first_pdf_file, 1, ll_pos)                // Include the final "\"
    
ELSE
    ls_err = "The first PDF file to be merged with the invoice has no PATH.~r~n Filename: " + as_first_pdf_file
    GOTO Exit_Function
END IF

// Split the second filename
ll_pos = LASTPOS (as_second_pdf_file, "\")
IF (ll_pos > 0) THEN
    
    ls_file [2] = MID (as_second_pdf_file, (ll_pos + 1), 9999)
    ls_path [2] = MID (as_second_pdf_file, 1, ll_pos)                // Include the final "\"
    
ELSE
    ls_err = "The second PDF file to be merged with the invoice has no PATH.~r~n Filename: " + as_second_pdf_file
    GOTO Exit_Function
END IF



// Merge the two PDF file using:    PDFtk_Pro
//                                  https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/
//                               sid.steward@pdflabs.com

// Example:  pdftk in1.pdf in2.pdf cat output out1.pdf
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ls_cmd = "pdftk "                                                    + &
            '"' + as_first_pdf_file  + '"  '                    + &
            '"' + as_second_pdf_file + '" cat output '    + &
            '"' + as_result_filename + '" '


// ll_rc = RUN (ls_cmd)
 

lb_exists = inv_runwait.runandwait (ls_cmd, inv_runwait.sw_shownormal)  

IF (lb_exists) THEN
    
    ls_msg = "We merged the PDF Notice with the invoice."
//    ls_err = "SUCCESS! We merged the two PDF files: ~r~n" + &
//                "~r~n Error:   " + inv_runwait.LastErrorText    + &
//                "~r~n File 1:  " + as_first_pdf_file             + &
//                "~r~n File 2:  " + as_second_pdf_file             + &
//                "~r~n Output:  " + as_result_filename          + &
//                "~r~n"                                                     + &
//                "~r~n LS_CMD = ~r~n " + ls_cmd
//                
//    MessageBox ("PDF Files Merged", ls_err)    
    
ELSE
    ls_msg = "Unable to merge the PDF Notice with the invoice."
//    ls_err = "Unable to merge the two PDF files: ~r~n"     + &
//                "~r~n Error:   " + inv_runwait.LastErrorText    + &
//                "~r~n File 1:  " + as_first_pdf_file             + &
//                "~r~n File 2:  " + as_second_pdf_file             + &
//                "~r~n Output:  " + as_result_filename          + &
//                "~r~n"                                                     + &
//                "~r~n LS_CMD = ~r~n " + ls_cmd
//                
//    MessageBox ("PDF Merge Error", ls_err, Exclamation!)
END IF
gnv_app.of_SetMicroHelp (ls_msg)


Exit_Function:
    RETURN ls_err





~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



//*********************************************************************
// Object        :    n_cst_pdf
// Function        :  of_wait_for_file_to_be_created
//
// Ancestor        :    Extended
// Access        :    Public
// Arguments    :    value   string    as_filename
//
// Returns        :    String              Error msg; Empty string = no error
// Throws        :    None
//
// Description    :    Wait until the specified file actually exists.
//
//********************************************************************
// Revision History
//
// Developer    Date              Version      Description
// ---------    -----------      ---------      -------------------------------
// O Knight    28-AUG-2022   4.0.5.241   #5440515:  Initial install.
//
//********************************************************************
// COPYRIGHT © 2022 CSG SYSTEMS INTERNATIONAL, INC. AND/OR ITS
// AFFILIATES (“CSG”). ALL RIGHTS RESERVED.
//********************************************************************

boolean    lb_exists
long        ll_seconds
string    ls_err
time        lt_start, lt_now



lt_start = NOW ()
lb_exists = FALSE
gnv_app.of_SetMicrohelp ("Waiting for the temp invoice file to be created....")

DO WHILE (NOT lb_exists)
    
    lb_exists = FileExists (as_filename)
    IF (NOT lb_exists) THEN
        
        lt_now     = Now ()
        ll_seconds = SecondsAfter (lt_start, lt_now)
        IF (ll_seconds > 60) THEN
            ls_err = "The specified file did not exist after 60 seconds. ~r~n" + &
                        "~r~n Temp File:  " + as_filename
            GOTO Exit_Function
        END IF
        
    END IF
    
LOOP
gnv_app.of_SetMicrohelp ("The file has been created: " + as_filename)



Exit_Function:
    IF (ls_err <> "") THEN
        MessageBox ("File Creation Error", ls_err,  Exclamation!)
    END IF
    RETURN ls_err
    
    
    
   

Comment
There are no comments made yet.
Armeen Mazda @Appeon Accepted Answer Pending Moderation
  1. Friday, 2 September 2022 22:22 PM UTC
  2. PowerBuilder
  3. # 2

Did you run the app as administrator?  I wonder if security restrictions are preventing it from executing.

Just FYI, PB 2022 has a new PDFBuilder that lets you do all sorts of things, such as merging PDFs: https://docs.appeon.com/pb/whats_new/New_PDFBuilder_objects.html

Comment
  1. Chris Pollach @Appeon
  2. Friday, 2 September 2022 22:48 PM UTC
I think that Armeen's postulation about running "As Admin" is correct as I see that you were in Admin mode when running in the Command Console (that worked).
  1. Helpful
  1. Andreas Mykonios
  2. Monday, 5 September 2022 06:23 AM UTC
Well, good news for you Olan. As mike mentioned here: https://community.appeon.com/index.php/qna/q-a/powerbuilder-2022-ga-tentative-date, I also see that build 1878 is available...

Andreas.
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.