firstred / postnl-api-php

PostNL API PHP bindings
https://postnl-php.readthedocs.io
MIT License
26 stars 37 forks source link

Getting the PDF label without saving it #17

Closed chris102948 closed 3 years ago

chris102948 commented 4 years ago

At this moment we create a shipment and get a PDF label:

        $labelFile = BP . self::LABELS_DIR . $this->barcode . '.pdf';
        $pdf = base64_decode($this->label->getResponseShipments()[0]->getLabels()[0]->getContent());
        file_put_contents($labelFile, $pdf);
        return $this;

However, this will result in a folder with a long list generated PDF's, so it does not look like a proper way to do this. Actually we're looking for a similar approach to the Sendcloud API where a label-URL can be requested through the API: https://docs.sendcloud.sc/api/v2/shipping/#get-a-pdf-label

Is there any similar way to do this with the PostNL API?

TomKriek commented 4 years ago

You can just output the base64decoded label as a file to save for the user, instead of saving it to disk locally.

zmip commented 4 years ago

A simple solution would be to clean up the cached PDF label files with a cron job. I personally like to keep them around for a short while.

chris102948 commented 4 years ago

Thank you for your replies. We ended up using TomKriek's solution and output the label together with some additional information as 1 merged PDF using FPDF

firstred commented 4 years ago

I am going to double-check if the library properly supports this after merging the labels. It should be as easy as:

<?php
$postnl = new PostNL(...);

header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=postnl-label-923323.pdf");
echo $postnl->getMergedLabels(); // Would prob. require some kind of __toString() implementation
exit;