× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



OK so here is the Python code to password protect the PDF.

It takes 3 parms, -i is the input file, -o is the output file and -p is the
password.

import PyPDF4, argparse, sys

# initiate the parser
parser = argparse.ArgumentParser()

# add long and short argument
parser.add_argument("--input", "-i", help="Input PDF name.")
parser.add_argument("--output", "-o", help="Output PDF name.")
parser.add_argument("--password", "-p", help="Password.")

# read arguments from the command line
args = parser.parse_args()

if args.input is None:
print("Input file name is required.")
sys.exit(-1)

if args.output is None:
print("Output file name is required.")
sys.exit(-1)

if args.password is None:
print("Password is required.")
sys.exit(-1)

pdfFile = open(args.input, 'rb')

# Create reader and writer object
pdfReader = PyPDF4.PdfFileReader(pdfFile)
pdfWriter = PyPDF4.PdfFileWriter()

# Add all pages to writer (accepted answer results into blank pages)
for pageNum in range(pdfReader.numPages):
pdfWriter.addPage(pdfReader.getPage(pageNum))

# Encrypt with your password
pdfWriter.encrypt(args.password)

# Write it to an output file. (you can delete unencrypted version now)
resultPdf = open(args.output, 'wb')

pdfWriter.write(resultPdf)

resultPdf.close()

And the RPG that I use to call this Python script.

//========================================================================================
// Jason Olson 07/01/2019 Initial program create.
//========================================================================================

//========================================================================================
// Copybooks
//========================================================================================

/copy qcpysrc,ops00cb00
/copy qcpysrc,ops00cb01
/copy qcpysrc,ops00cb02
/copy qcpysrc,ops00cb04
/copy qcpysrc,ops00cb14
/copy qcpysrc,ops00cb21

//========================================================================================
// Main(); Password protect a PDF file. Calls python script PasswordPDF.py
//========================================================================================

dcl-pr Main ExtPgm('OPS04R02');
InputFile char(3000);
OutputFile char(3000);
Password char(128);
Error likeds(ErrorDetail2);
end-pr;

//========================================================================================
// Begin Main();
//========================================================================================

dcl-proc Main;
dcl-pi Main;
InputFile char(3000);
OutputFile char(3000);
Password char(128);
Error likeds(ErrorDetail2);
end-pi;

// Get job details.

GetJobDetails( JobDetail2
: %size(JobDetail2)
: 'JOBI0600'
: '*'
: *blanks
: ErrorDetail2
);

// Add an environment variable so that the END SESSION window does not
appear.

Command = 'ADDENVVAR ENVVAR(QIBM_QSH_CMD_OUTPUT) VALUE(NONE) LEVEL(*JOB)
REPLACE(*YES)';

RunCommand( Command
: %len(Command)
: ControlBlock
: %size(ControlBlock)
: 'CPOP0100'
: 'Command'
: 0
: 0
: ErrorDetail2
);

// Add an environment variable so the program can locate Python3.

Command = 'ADDENVVAR '
+ 'ENVVAR(PATH) '
+ 'VALUE('
+ Quote+'/QOpenSys/pkgs/bin'+Quote
+ ') '
+ 'LEVEL(*JOB) '
+ 'REPLACE(*YES)';

RunCommand( Command
: %len(Command)
: ControlBlock
: %size(ControlBlock)
: 'CPOP0100'
: 'Command'
: 0
: 0
: ErrorDetail2
);

// Build command to call the python script.

Command = 'QSH '
+ 'CMD('
+ Quote
+ 'python3 /Ops/Python/PasswordPDF.py '
+ '-i '
+ %trim(InputFile) + ' '
+ '-o '
+ %trim(OutputFile) + ' '
+ '-p '
+ %trim(Password)
+ Quote
+ ')';

RunCommand( Command
: %len(Command)
: ControlBlock
: %size(ControlBlock)
: 'CPOP0100'
: 'Command'
: 0
: 0
: ErrorDetail2
);

GetMessage( MessageDetail1
: %size(MessageDetail1)
: 'RCVM0300'
: 'MAIN'
: 0
: '*LAST'
: *blanks
: 0
: '*SAME'
: ErrorDetail2
);

MessageText = %subst( MessageDetail1.Message
: MessageDetail1.LengthOfReplacementDataReturned + 1
: MessageDetail1.LengthOfMessageReturned
);

select; // Could not create the password protected PDF. Set return
message ID.

when %subst(MessageText:41:1) <> '0';

Error.MessageID = 'O000007';

endsl;

end-proc Main;

//========================================================================================
// End Main();
//========================================================================================
Thanks,

Jason E. Olson
IBM i Engineer/Developer
josys36@xxxxxxxxx
480.223.2952


On Mon, Jul 1, 2019 at 7:37 AM John Rusling via RPG400-L <
rpg400-l@xxxxxxxxxxxxxxxxxx> wrote:

Hi Jose, i would love to see the rpgle executing python piece of code, if
you don't mind.
(for my own betterment)

John


<snip>
Some time ago i have the same issue.
I decided install ibm i python. And now this is my run sequence:
1º RPGLE program generate a pfd file in a ifs folder,
2º The same RPGLE program execute a python script witch generate a copy of
pdf file (because i need so) protected with one password received by
parameter in the script.

First of all you need install python, and after build a script that work
for you.

it isn't complicated.
</snip>
<br />
The information in this email is confidential and may be legally
privileged.
It is intended solely for the addressee. Access to this email by anyone
else is
unauthorized. If you are not the intended recipient, any disclosure,
copying,
distribution or any action taken or omitted to be taken in reliance on it,
is
prohibited and may be unlawful.
--
This is the RPG programming on IBM i (RPG400-L) mailing list
To post a message email: RPG400-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/rpg400-l.

Please contact support@xxxxxxxxxxxx for any subscription related
questions.

Help support midrange.com by shopping at amazon.com with our affiliate
link: https://amazon.midrange.com


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.