From 929c1b90d6b16706f9c63339f5f2a2b42332f3f3 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Wed, 6 Aug 2025 15:21:03 +0200 Subject: split into two standalone containers --- print/print.php | 147 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 print/print.php (limited to 'print/print.php') diff --git a/print/print.php b/print/print.php new file mode 100644 index 0000000..0e1137f --- /dev/null +++ b/print/print.php @@ -0,0 +1,147 @@ + $matches[1][$idx], + 'name' => $matches[2][$idx], + 'set' => strtoupper($matches[3][$idx]), + 'number' => $matches[4][$idx], + ]; +} + +if (empty($cards)) { + die("Couldn't find any cards matching the pattern."); +} + +/* + * Select data from DB + */ + +$db = new \PDO('sqlite:' . __DIR__ . '/AllPrintings.sqlite'); + +$images = []; +foreach ($cards as $card) { + $query = <<prepare($query); + $statement->execute(['setCode' => $card['set'], 'number' => $card['number']]); + $result = $statement->fetch(); + $id = $result['scryfallId'] ?? ''; + + if (empty($id)) { + $query = <<prepare($query); + $statement->execute(['setCode' => $card['set'], 'number' => $card['number']]); + $result = $statement->fetch(); + $id = $result['scryfallId'] ?? ''; + + if (empty($id)) { + echo "$card[name] ($card[set]) $card[number] not found."; + echo php_sapi_name() == 'cli' ? "\n" : '
'; + continue; + } + } + + $images[] = [ + 'quantity' => $card['quantity'], + 'src' => "https://cards.scryfall.io/png/front/" . substr($id, 0, 1) . "/" . substr($id, 1, 1) . "/$id.png", + 'alt' => $card['name'], + ]; + + if (in_array($result['layout'] ?? '', ['transform', 'double_faced_token'])) { + $images[] = [ + 'quantity' => $card['quantity'], + 'src' => "https://cards.scryfall.io/png/back/" . substr($id, 0, 1) . "/" . substr($id, 1, 1) . "/$id.png", + 'alt' => $card['name'], + ]; + } +} + +if (empty($images)) { + die('No cards to print.'); +} + +/* + * Build HTML + */ + +$template = << + + + + +
{{imgs}}
+ + +HTML; + +$imgs = array_map(function ($image) { + return str_repeat("\"$image[alt]\"", $image['quantity']); +}, $images); + +$html = str_replace('{{imgs}}', implode('', $imgs), $template); + +/* + * Build PDF + */ + +$request = Gotenberg::chromium('gotenberg:3000')->pdf() + ->paperSize(8.27, 11.7) # A4 + ->margins(0.3, 0, 0.3, 0) + ->outputFilename(date("Ymd_His")) + ->html(Stream::string('index.html', $html)); + +if (php_sapi_name() === 'cli') { + @mkdir(__DIR__ . '/output'); + Gotenberg::save($request, __DIR__ . '/output'); +} else { + $response = Gotenberg::send($request); + + header('Content-Type: application/pdf'); + header('Content-Disposition: attachment; filename="' . date("Ymd_His") . '"'); + echo $response->getBody(); +} -- cgit v1.2.3