问题
When trying to use TCPDF to generate a pdf and sign it, i get the error:
openssl_pkcs7_sign(): error getting private key.
and:
Undefined offset: 1 in ..\tcpdf\tcpdf.php on line 7604
I have found the following stackoverflow posts and tried to follow their solutions:
- Warning: openssl_pkcs7_sign(): error getting private key using WAMP
- Warning: openssl_pkcs7_sign() [function.openssl-pkcs7-sign]: error getting private key in C:\xampp\htdocs\this\tcpdf\tcpdf.php on line 8366
Unfortunately, nothing works.
I have tried to use:
$certificate = "file://" . getcwd() . "/objects/dummy_cert.cert";
or
$certificate = "file://" . __DIR__ "/objects/dummy_cert.cert";
or writing the absolute path by hand.
My script is executed from the project root path, from which /objects/
is the next folder in which my dummy_cert.cert
lies.
What is happening here and how can i fix this?
I even logged the content of $certificate to be sure it generates the correct path. The generated path, copied into Google Chrome address bar, immediately loads the content of the .cert file.
回答1:
You need the 'file://' prefix and the realpath to file:
//in your case
$certificate = 'file://'.realpath('/objects/dummy_cert.cert');
// OR for other cases
$certificate = 'file://'.realpath('../upper_folder/objects/dummy_cert.cert');
// OR
$certificate = 'file://'.realpath('C:/objects/dummy_cert.cert');
来源:https://stackoverflow.com/questions/40160628/openssl-pkcs7-sign-error-getting-private-key