Print a PDF from your computer to a cloud printer
In this tutorial, I'll show you how to build a web page that sends a PDF file from your computer to a remote printer using the Expedy Print service.
The example uses a label printer to print adhesive shipping labels in the standard 4×6 format (10×15 cm) — for instance, labels generated from an e-commerce platform, sent to a printer in another area of your warehouse, or to a dropshipping partner on the other side of the world.
The form also includes a text field to generate a PDF on the fly. Both features (file upload and text-to-PDF) can be used independently or together.
Full source code: github.com/ExpedyDev/pdftoprinter
Requirements
Hardware
- A thermal printer (this example uses a Netum NT-LP110F, but Zebra, Brother, BeePRT and other models are compatible, including A3/A4/photo formats)
- An Expedy Cloud Print adapter
- A standard internet connection
Web
1. Creating the HTML form
The form includes two fields:
- A text field to generate a PDF on the fly
- A file picker to upload one or more existing PDF files
<!DOCTYPE html>
<html>
<head>
<title>Create a PDF and print</title>
</head>
<body>
<h1>Create a PDF and print</h1>
<form method="post" action="" enctype="multipart/form-data">
<label for="text">Enter the text to print as PDF:</label><br>
<input type="text" id="text" name="text"><br><br>
<label for="files">Upload PDF files:</label><br>
<input type="file" id="files" name="files[]" multiple><br><br>
<button type="submit" name="submit">Print</button>
</form>
</body>
</html>
2. PHP code
Here is the complete PHP code with inline comments:
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['text']) || !empty($_FILES['files']['tmp_name'][0])) {
$text = $_POST['text'];
$pdfName = "pdf_" . date('Ymd_His') . ".pdf";
require('fpdf/fpdf.php');
require('fpdi/src/autoload.php');
// 4×6 format (105×148 mm)
$pdf = new \setasign\Fpdi\Fpdi('P', 'mm', array(105, 148));
// Merge uploaded PDF files
if (!empty($_FILES['files']['tmp_name'][0])) {
foreach ($_FILES['files']['tmp_name'] as $file) {
if (!empty($file) && file_exists($file)) {
$pageCount = $pdf->setSourceFile($file);
for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
$tplIdx = $pdf->importPage($pageNumber);
$pdf->AddPage();
$pdf->useTemplate($tplIdx);
}
}
}
}
// Add a page from the text field
if (!empty($text)) {
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(40, 10, $text);
}
$pdf->Output('F', $pdfName);
$file_url = 'https://' . $_SERVER['HTTP_HOST'] . '/' . $pdfName;
sendToExpedyPrinter($file_url);
echo "<p>Your files have been sent to the printer!</p>";
} else {
echo "<p>No text or PDF file specified.</p>";
}
}
function sendToExpedyPrinter($file_url) {
$printer_uid = 'ENTER_PRINTER_UID';
$data = array(
'usb_msg' => $file_url,
'origin' => 'Your defined origin tag.. a uri, a name ..'
);
$options = array(
CURLOPT_URL => "https://www.expedy.fr/api/v2/devices/" . $printer_uid . "/usb/4/print",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => array(
"Accept: application/json",
"Authorization: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"Content-Type: application/json"
),
);
$curl = curl_init();
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #: " . $err;
} else {
echo $response;
}
}
?>
Key points
FPDF and FPDI libraries
Both libraries are required: FPDF handles PDF generation, while FPDI merges multiple files into a single document. This merging step matters — it is more efficient for the printer to receive one multi-page PDF than several separate files sent one by one to the API.
Text formatting
If the text field is filled in, an extra page is appended to the generated PDF. The font can be customised here:
$pdf->SetFont('Arial', 'B', 16);
PDF dimensions
This example uses the 4×6 format (105×148 mm) for shipping labels. Width and height can be adjusted to match any paper format:
$pdf = new \setasign\Fpdi\Fpdi('P', 'mm', array(105, 148));
Printer UID
Retrieve your printer's unique identifier from the Expedy console under Printers. Do not include the #. Example: GSEPOHGEIO.
$printer_uid = 'ENTER_PRINTER_UID';
API keys
Available in the Expedy console under API Keys. Format: sid:token.
"Authorization: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
USB port
In this example, the printer is connected on USB port 4. To identify the correct port, go to the Expedy console > Machines > USB SCAN.
CURLOPT_URL => "https://www.expedy.fr/api/v2/devices/" . $printer_uid . "/usb/4/print",
Testing a print job
If everything is set up correctly, a PDF file will be created in your root folder and the API will return a confirmation response.
⚠️ Note: generated PDF files are not deleted automatically. They can take up significant disk space on your hosting over time.