PHP: Save 'dynamic text or pdf content' in google cloud storage?

前端 未结 1 550
死守一世寂寞
死守一世寂寞 2021-01-28 06:17

runtime: php

GCS file uploading process:

$storage = new StorageClient();
$file = fopen($source, \'r\');
$bucke         


        
相关标签:
1条回答
  • 2021-01-28 07:11

    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.
    
    0 讨论(0)
提交回复
热议问题