Auto-Download or Auto-print label after creation
Teyk0o opened this issue · 9 comments
Hi !
When i would like to echo all parcels this error appear :
The code :
<?php
use Picqer\Carriers\SendCloud\Connection;
use Picqer\Carriers\SendCloud\SendCloud;
require_once ".././vendor/autoload.php";
$connection = new Connection("apiKeyHideForGithub", "apiKeyHideForGithub");
$sendCloudClient = new SendCloud($connection);
$parcels = $sendCloudClient->parcels()->all();
echo $parcels;
The line number 12 is the echo.
Last note : How to automatically download the label ?
if ($parcel->save()) {
$labelUrl = $parcel->getPrimaryLabelUrl();
$documentDownloader = new DocumentDownloader($connection);
$labelContents = $documentDownloader->getDocument($labelUrl, 'pdf');
}
I take this, my label pass in "Printed" but he was not downloaded.
Hi @Teyk0o you can do it this way:
$this->Parcel->save();
$labelUrl = $this->Parcel->getPrimaryLabelUrl();
$documentDownloader = new DocumentDownloader($connection);
$labelContents = $documentDownloader->getDocument($labelUrl, 'pdf');
return $this->response
->withType('application/pdf')
->withStringBody($labelContents)
->withDownload('document_name.pdf');
Hi @magic-77 ! Thanks a lot for your response, i will test this now and i comeback to tell you if that work 👍🏻
@magic-77 i have an error with this code :
<?php
use Picqer\Carriers\SendCloud\Connection;
use Picqer\Carriers\SendCloud\DocumentDownloader;
use Picqer\Carriers\SendCloud\Parcel;
use Picqer\Carriers\SendCloud\SendCloud;
require_once ".././vendor/autoload.php";
class shipping
{
function createCommand(Parcel $parcel) {
$connexion = new Connection('e8e27fdf8fb14d26b9d54b9b473bfbb7', '9b1605e6574943da960faac7069a5652');
$parcel->save();
$labelUrl = $parcel->getPrimaryLabelUrl($connexion);
$documentDownloader = new DocumentDownloader($connexion);
$labelContents = $documentDownloader->getDocument($labelUrl, 'pdf');
return $this->response
->withType('application/pdf')
->withStringBody($labelContents)
->withDownload('document_name.pdf');
}
}
Warning: Undefined property: shipping::$response in C:\Users\Theov\Desktop\HerbaltiProject\php\shipping.php on line 19
I call this function here :
<?php
use Picqer\Carriers\SendCloud\Connection;
use Picqer\Carriers\SendCloud\DocumentDownloader;
use Picqer\Carriers\SendCloud\SendCloud;
include ".././php/shipping.php";
require_once ".././vendor/autoload.php";
$ship = new shipping();
$connection = new Connection('e8e27fdf8fb14d26b9d54b9b473bfbb7', '9b1605e6574943da960faac7069a5652');
$sendcloudClient = new SendCloud($connection);
$parcel = $sendcloudClient->parcels();
$parcel->shipment = 8; // Shipping method, get possibilities from $sendCloud->shippingMethods()->all()
$parcel->name = 'John Smith';
$parcel->company_name = 'ACME';
$parcel->address = 'Wellingtonstreet 25';
$parcel->city = 'Wellington';
$parcel->postal_code = '18100';
$parcel->country = 'FR';
$parcel->order_number = 'test2910-52321';
$parcel->requestShipment = true; // Specifically needed to create a shipment after adding the parcel
$ship->createCommand($parcel);
@Teyk0o ah sorry, i forgot to mention that my example is used within CakePHP.
If you don't use any Framework than it is necessary to set the required headers manually
you can try this (not checked)
header('Content-Description: File Transfer');
header('Content-Disposition: attachment;filename="document.pdf"');
header('Content-Type: application/pdf');
header('Content-Length: '.strlen($labelContents));
header('Pragma: public');
or you use Guzzle
https://docs.guzzlephp.org/en/stable/psr7.html#streams
Hi @magic-77 thanks a lot for your response !
That work, the pdf was automatically downloaded but is empty :
@Teyk0o ah sorry, i forgot to mention that my example is used within CakePHP. If you don't use any Framework than it is necessary to set the required headers manually
you can try this (not checked)
header('Content-Description: File Transfer'); header('Content-Disposition: attachment;filename="document.pdf"'); header('Content-Type: application/pdf'); header('Content-Length: '.strlen($labelContents)); header('Pragma: public');
or you use Guzzle https://docs.guzzlephp.org/en/stable/psr7.html#streams
<?php
use Picqer\Carriers\SendCloud\Connection;
use Picqer\Carriers\SendCloud\DocumentDownloader;
use Picqer\Carriers\SendCloud\SendCloud;
require_once ".././vendor/autoload.php";
$connection = new Connection('e8e27fdf8fb14d26b9d54b9b473bfbb7', '9b1605e6574943da960faac7069a5652');
$sendcloudClient = new SendCloud($connection);
$parcel = $sendcloudClient->parcels();
$parcel->shipment = 8; // Shipping method, get possibilities from $sendCloud->shippingMethods()->all()
$parcel->name = 'Théo';
$parcel->company_name = 'Herbalti';
$parcel->address = '32 rue Jean Moulin';
$parcel->city = 'Paris';
$parcel->postal_code = '42000';
$parcel->country = 'FR';
$parcel->order_number = 'testtheo-52321';
$parcel->requestShipment = true; // Specifically needed to create a shipment after adding the parcel
if ($parcel->save()) {
$labelUrl = $parcel->getPrimaryLabelUrl();
$documentDownloader = new DocumentDownloader($connection);
$labelContents = $documentDownloader->getDocument($labelUrl, 'pdf');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment;filename="document.pdf"');
header('Content-Type: application/pdf');
header('Content-Length: '.strlen($labelContents));
header('Pragma: public');
}
Work but empty PDF
<?php
use Picqer\Carriers\SendCloud\Connection;
use Picqer\Carriers\SendCloud\DocumentDownloader;
use Picqer\Carriers\SendCloud\SendCloud;
require_once ".././vendor/autoload.php";
$connection = new Connection('e8e27fdf8fb14d26b9d54b9b473bfbb7', '9b1605e6574943da960faac7069a5652');
$sendcloudClient = new SendCloud($connection);
$parcel = $sendcloudClient->parcels();
$parcel->shipment = 8; // Shipping method, get possibilities from $sendCloud->shippingMethods()->all()
$parcel->name = 'Théo';
$parcel->company_name = 'Herbalti';
$parcel->address = '32 rue Jean Moulin';
$parcel->city = 'Vierzon';
$parcel->postal_code = '42000';
$parcel->country = 'FR';
$parcel->order_number = 'testtheo-52321';
$parcel->requestShipment = true; // Specifically needed to create a shipment after adding the parcel
if ($parcel->save()) {
$labelUrl = $parcel->getPrimaryLabelUrl();
$documentDownloader = new DocumentDownloader($connection);
$labelContents = $documentDownloader->getDocument($labelUrl, 'pdf');
$name = 'delivery_label_herbalti.pdf';
header('Content-Type: application/pdf');
header('Content-Length: '.strlen( $labelContents ));
header('Content-disposition: inline; filename="' . $name . '"');
header('Cache-Control: no-cache, max-age=0');
header('Pragma: no-cache');
echo $labelContents;
}
This code WORK ! With no framework :D