Home

Create download URLs on your own website

Customers may hesitate to follow links from an unfamiliar domain. Deliver PDFs from your own domain, for example https://download.yourdomain.com/download/ID, rather than https://tailorpdf.com/download/ID.

The following script on your subdomain fetches PDFs from TailorPDF and delivers them through your website, so recipients do not need to recognise TailorPDF's domain.

Requirements

  • PHP 8.3 or later with cURL.
  • A subdomain such as download.yourdomain.com or pdf.yourdomain.com. A separate subdomain is usually preferable and needs an HTTPS certificate; free certificates are available.
  • A server that permits URL rewrites from /download/ID to /get.php?id=ID.

The script handles the remaining work. Save it as get.php and set the shown rewrite rule in your subdomain's .htaccess.

<?php
// Name der Datei: get.php
// Name of file: get.php

// 2025 TailorPDF @ Rösch Informatik GmbH

// TailorPDF PDF Weiterleitungs-Skript
// Anforderungen PHP mit cURL Erweiterung wird benötigt.
// Stelle dieses Script an eine Subdomain wie pdf.yourdomain.com
// setze .htaccess so, dass alle Anfragen an dieses Script weitergeleitet werden.
// Beispiel .htaccess Regel:
//    RewriteEngine On 
//    # forward of /download/ID to get.php?id=ID
//    RewriteRule ^download/([a-zA-Z0-9]+)$ get.php?id=$1 [L,QSA]

// TailorPDF redirecting PDF script
// Requires PHP with cURL extension.
// set this script to a subdomain like pdf.yourdomain.com
// set .htaccess to forward all requests to this script.
// example .htaccess rule:
//    RewriteEngine On 
//    # forward of /download/ID to get.php?id=ID
//    RewriteRule ^download/([a-zA-Z0-9]+)$ get.php?id=$1 [L,QSA]

// ID aus URL holen, get Parameter id
$id = $_GET['id'] ?? '';

// Validierung: Nur alphanumerische Zeichen erlauben
// validation of id: only alphanumeric characters allowed
if (empty($id) || !preg_match('/^[a-zA-Z0-9]+$/', $id)) {
    http_response_code(400);
    die('Invalid ID');
}

// Ziel-URL konstruieren
// construct target URL
$targetUrl = "https://tailorpdf.com/download/" . $id;

// HTTP-Header für die Weiterleitung der PDF-Datei
// HTTP headers for fetching the PDF file
$ch = curl_init($targetUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

// Header-Funktion zum Abfangen der Original-Header
// header function to capture original headers
$headers = [];
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($curl, $header) use (&$headers) {
    $len = strlen($header);
    $header = explode(':', $header, 2);
    if (count($header) < 2) return $len;
    
    $key = strtolower(trim($header[0]));
    $value = trim($header[1]);
    $headers[$key] = $value;
    
    return $len;
});

// PDF-Inhalt abrufen
// fetch PDF content
$pdfContent = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// Fehlerbehandlung
// error handling
if ($httpCode !== 200 || $pdfContent === false) {
    http_response_code(404);
    die('PDF not found - wrong ID <br>PDF not found - wrong ID');
}

// Dateinamen aus Content-Disposition Header extrahieren
// extract filename from Content-Disposition header
$filename = 'document.pdf'; // Fallback

if (isset($headers['content-disposition'])) {
    // Beispiel: attachment; filename="document_customer_123456.pdf"
    // oder: inline; filename="document_customer_123456.pdf"
    // Example: attachment; filename="document_customer_123456.pdf"
    // or: inline; filename="document_customer_123456.pdf"
    if (preg_match('/filename=["\']?([^"\';\r\n]+)["\']?/i', $headers['content-disposition'], $matches)) {
        $filename = $matches[1];
    }
}

// Header für PDF-Download setzen
// set headers for PDF download
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Length: ' . strlen($pdfContent));
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');

// PDF ausgeben
// output PDF
echo $pdfContent;