PHP: Save 'dynamic text or pdf content' in

2020-04-30 18:40发布

问题:

runtime: php

GCS file uploading process:

$storage = new StorageClient();
$file = fopen($source, 'r');
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($file, [
  'name' => $objectName
]);

Now i want to create object with dynamic text/pdf instead of using $source path file.

for example:

$storage = new StorageClient();
$objectName = "newfile.txt";
$content = "This is the dynamic content";
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($content, [
    'name' => 'textDocs/'.$objectName
]);

so it'll create new object as "newfile.txt" with content "This is the dynamic content".

is there any cloud libraries or functions to process this?
thanks in advance

回答1:

For Dynamic creation of text document (with-dynamic-content) in Google-cloud-storage, below code will be work perfectly.

$storage = new StorageClient();
$objectName = "newfile.txt";                  // name of object/text-file
$content = "This is the dynamic content";     // assign static/dynamic data to $content variable
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($content, [
    'name' => 'textDocs/'.$objectName
]);

For Dynamic creation of pdf document (with-dynamic-content) in Google-cloud-storage.

$name = "samplePDF.pdf";
$p = new PDFTable("L");
$p->AddPage();
$p->setfont('helvetica','',12);
$p->htmltable($html);
$pdfData = $p->output("",'S'); // returns pdf as string
$filename = "pdfDocs/".$name;
/** 
 * file uploading into google cloud storage : start
 */
$storage->bucket($bucketName, true)->upload(
    $pdfData,
    [
    'name' => $filename,
    'predefinedAcl' => 'publicRead'
    ]
);
/** 
 * file uploading into google cloud storage : end
 */
$p->output($name,"D"); // D->downloads file in our system.