num-num/ubl-invoice

Attaching a PDF with binary data

Closed this issue · 2 comments

avatar commented

Hi,
I'm trying to attach a file to an ubl file. This works when i have my file locally:

$ublInvoice->setAdditionalDocumentReference(
            (new AdditionalDocumentReference())
            //->setDocumentType('application/pdf')
            ->setAttachment(
                (new Attachment())
                ->setFilePath(
                    getcwd()."/test.pdf"
                )
            )
        );

Now i'm storing my files on amazon s3, i'm using flysystem to store and read my files from amazon.
How do I attach a file without having to pass the path, but instead passing the binary data?

this will do the job

class Attachment extends \NumNum\UBL\Attachment
{
    private $filestream;

    private $filename;

    public function setFileStream($filename, $filestream): void
    {
        $this->filestream = $filestream;
        $this->filename = $filename;
    }

    /**
     * The xmlSerialize method is called during xml writing.
     */
    public function xmlSerialize(Writer $writer): void
    {
        if ($this->filestream) {
            $fileContents = $this->filestream;
            $mimeType = 'application/pdf';

            $data = [
                'name' => Schema::CBC.'EmbeddedDocumentBinaryObject',
                'value' => $fileContents,
                'attributes' => [
                    'mimeCode' => $mimeType,
                    'filename' => $this->filename,
                ],
            ];

            $writer->write($data);
        }
    }
}
avatar commented

Thanks, that seems to work!