英文:
how can I merge base64 pdf files into one using Laravel?
问题
I have one issue and I would like to know how to fix it.
I have an api that returns base64 pdfs:
{
"resp_1": "JWETRi0xLjMNCiXi48/TDQoNCjEgMCB.....",
"resp_2": "RTGHEgi0xLhjhjjjDQoNCjEgMCB.....",
}
I need to merge them into one file and upload them to S3, for uploading files there is no problem, but the issue here is merging. I am trying this:
...
use Webklex\PDFMerger\Facades\PDFMergerFacade as PDFMerger;
....
//$resp_1 is JWETRi0xLjMNCiX.....
$file_encoded = base64_decode($resp_1);
$file_encoded_2 = base64_decode($resp_2);
$oMerger = PDFMerger::init();
$oMerger->addPDF(mb_convert_encoding($file_encoded, 'UTF-8', 'UTF-8'), 'all');
$oMerger->addPDF(mb_convert_encoding($file_encoded_2, 'UTF-8', 'UTF-8'), 'all');
$oMerger->merge();
$oMerger->save(public_path('storage/merged.pdf'));
.....
{ "message": "Could not locate PDF on '%PDF-1.3\r\n%????.......",
"file": "D:\test\vendor\webklex\laravel-
pdfmerger\src\PDFMerger\PDFMerger.php", "line": 184, "trace": [
]
What can I do?
英文:
I have one issue and I would like to know how to fix it.
I have an api that returns base64 pdfs:
{
"resp_1": "JWETRi0xLjMNCiXi48/TDQoNCjEgMCB.....",
"resp_2": "RTGHEgi0xLhjhjjjDQoNCjEgMCB.....",
}
I need to merge them into one file and upload them to S3, for uploading files there is no problem, but the issue here is merging. I am trying this:
...
use Webklex\PDFMerger\Facades\PDFMergerFacade as PDFMerger;
....
....
//$resp_1 is JWETRi0xLjMNCiX.....
$file_encoded = base64_decode($resp_1);
$file_encoded_2 = base64_decode($resp_2);
$oMerger = PDFMerger::init();
$oMerger->addPDF(mb_convert_encoding($file_encoded, 'UTF-8', 'UTF-8'), 'all');
$oMerger->addPDF(mb_convert_encoding($file_encoded_2, 'UTF-8', 'UTF-8'), 'all');
$oMerger->merge();
$oMerger->save(public_path('storage/merged.pdf'));
.....
> { "message": "Could not locate PDF on '%PDF-1.3\r\n%????......."
> "file": "D:\test\vendor\webklex\laravel-
> pdfmerger\src\PDFMerger\PDFMerger.php", "line": 184, "trace": [
>
What can I do?
答案1
得分: 1
使用addString
方法替代addPDF
(addPDF
接受文件路径作为参数):
$oMerger->addString('原始文件数据', 'all');
英文:
Use addString
method instead of addPDF
(addPDF
accept a path to file as an argument):
$oMerger->addString('raw file data', 'all');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论