我正在使用mpdf库将HTML转换为PDF,并成功地将我的pdf文件存储在本地和远程服务器上。但是我不想把我的pdf文件存储在我的代码库服务器上,而是喜欢使用google cloud上可用的存储桶。
/*
*/
private function generatePDF($params, $quotationId) {
$location = '/var/www/html/development/pdfs/';
$html = $this->load->view('quotation', $data, TRUE);
$filename = "quo_" .time() . ".pdf";
$mpdf = new \Mpdf\Mpdf(['mode' => 'en-IN', 'format' => 'A4']);
$mpdf->WriteHTML($html);
$mpdf->SetHTMLFooter('<p style="text-align: center; text-size: 12px;">This is computer generated quotation. It does not require signature.</p>');
$pdf = $mpdf->Output($location . $filename, 'F');
$this->UploadModel->upload($pdf, $filename);
}
public function upload($pdf, $pdfName) {
$storage = new StorageClient();
$bucket = $storage->bucket("bucketname");
$object = $bucket->upload($pdf, ['name' => $pdfName]);
$object = $bucket->object($pdfName);
$object->update(['acl' => []], ['predefinedAcl' => 'PUBLICREAD']);
}在这里,我使用了'F‘类型,它将pdf文件保存在云服务器上我的代码仓库中创建的pdfs文件夹中,但我想直接将其存储到Google云存储桶中。
我没有太多关于谷歌云和mpdf库的经验,所以寻找帮助和指导来实现这些功能。
请帮帮我。
发布于 2018-08-10 19:57:28
我看到你在使用Cloud Storage Client Libraries。
首先,您需要将其安装到您的计算机上:
composer require google/cloud-storage然后,您需要按照指南执行set up authentication。
一旦这些被设置为create a bucket来存储PDF。
然后用documentation中的代码替换您的上传函数
use Google\Cloud\Storage\StorageClient;
/**
* Upload a file.
*
* @param string $bucketName the name of your Google Cloud bucket.
* @param string $objectName the name of the object.
* @param string $source the path to the file to upload.
*
* @return Psr\Http\Message\StreamInterface
*/
function upload_object($bucketName, $objectName, $source)
{
$storage = new StorageClient();
$file = fopen($source, 'r');
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($file, [
'name' => $objectName
]);
printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), $bucketName, $objectName);
}发布于 2018-11-26 15:04:47
我也遇到了同样的问题&推出了this solution,希望它能对你有所帮助。
使用“S”而不是“F”参数,这样它将返回字符串数据并将此数据直接传递到upload方法中。
https://stackoverflow.com/questions/51765665
复制相似问题