如何使用Laravel将Base64 PDF文件合并为一个文件?

huangapple go评论62阅读模式
英文:

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方法替代addPDFaddPDF接受文件路径作为参数):

$oMerger->addString('原始文件数据', 'all');
英文:

Use addString method instead of addPDF (addPDF accept a path to file as an argument):

$oMerger->addString('raw file data', 'all');

huangapple
  • 本文由 发表于 2023年5月22日 08:45:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定