diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2025-08-06 15:21:03 +0200 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2025-08-06 15:21:03 +0200 |
commit | 929c1b90d6b16706f9c63339f5f2a2b42332f3f3 (patch) | |
tree | b2057dc317f6e4476d828f103046c3ab7e834c45 /mtg-pdf.php | |
parent | d6adc45f0e0380d7f5404549e3774a02b2afaebe (diff) |
split into two standalone containers
Diffstat (limited to 'mtg-pdf.php')
-rw-r--r-- | mtg-pdf.php | 147 |
1 files changed, 0 insertions, 147 deletions
diff --git a/mtg-pdf.php b/mtg-pdf.php deleted file mode 100644 index 55b8112..0000000 --- a/mtg-pdf.php +++ /dev/null @@ -1,147 +0,0 @@ -<?php - -use Gotenberg\Gotenberg; -use Gotenberg\Stream; - -$decklist = $_ENV['DECKLIST']; -if (empty($decklist)) { - die('No decklist provided.'); -} - -require_once __DIR__ . '/vendor/autoload.php'; - -/* - * Match cards from input - */ - -preg_match_all("/(\d+) (.+) \(([\w\d]+)\) (\d+\w*) ?([\*\w]*)/", $decklist, $matches); -$cards = []; -for ($idx = 0; $idx < count($matches[0]); $idx++) { - $cards[] = [ - 'quantity' => $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:AllPrintings.sqlite'); - -$images = []; -foreach ($cards as $card) { - $query = <<<SQL - SELECT cardIdentifiers.scryfallId, cards.layout - FROM cardIdentifiers - JOIN cards ON cardIdentifiers.uuid=cards.uuid - WHERE cards.setCode=:setCode AND cards.number=:number - SQL; - $statement = $db->prepare($query); - $statement->execute(['setCode' => $card['set'], 'number' => $card['number']]); - $result = $statement->fetch(); - $id = $result['scryfallId'] ?? ''; - - if (empty($id)) { - $query = <<<SQL - SELECT tokenIdentifiers.scryfallId, tokens.layout - FROM tokenIdentifiers - JOIN tokens ON tokenIdentifiers.uuid=tokens.uuid - WHERE tokens.setCode=:setCode AND tokens.number=:number - SQL; - $statement = $db->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" : '<br>'; - 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 = <<<HTML -<html> - <head> - <style> - html, body { - padding: 0; - margin: 0; - } - - img { - width: 2.49in; - height: 3.48in; - display: block; - background-color: #16130e; - background-color: #000; - } - - #bg { - display: flex; - flex-wrap: wrap; - } - </style> - </head> - <body> - <div id="bg">{{imgs}}</div> - </body> -</html> -HTML; - -$imgs = array_map(function ($image) { - return str_repeat("<img src=\"$image[src]\" alt=\"$image[alt]\">", $image['quantity']); -}, $images); - -$html = str_replace('{{imgs}}', implode('', $imgs), $template); - -/* - * Build PDF - */ - -$request = Gotenberg::chromium('gotenberg:3000') - ->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(); -} |