From 5d55842c465710c910735bb6a55fcb42f910984e Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 26 Nov 2023 10:28:25 +0100 Subject: update commit --- mtg-pdf.php | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 mtg-pdf.php (limited to 'mtg-pdf.php') diff --git a/mtg-pdf.php b/mtg-pdf.php new file mode 100644 index 0000000..9b705b0 --- /dev/null +++ b/mtg-pdf.php @@ -0,0 +1,128 @@ + $matches[1][$idx], + 'name' => $matches[2][$idx], + 'set' => $matches[3][$idx], + 'number' => $matches[4][$idx], + ]; +} + +/* + * Select data from DB + */ + +$db = new \PDO('sqlite:AllPrintings.sqlite'); + +$images = []; +foreach ($cards as $card) { + $query = 'SELECT scryfallId,layout from cardIdentifiers JOIN cards on cardIdentifiers.uuid=cards.uuid WHERE cards.setCode=:setCode AND cards.number=:number'; + $statement = $db->prepare($query); + $statement->execute(['setCode' => $card['set'], 'number' => $card['number']]); + $result = $statement->fetch(); + $id = $result['scryfallId'] ?? ''; + + if (empty($id)) { + $query = 'SELECT scryfallId,layout from tokenIdentifiers JOIN tokens on tokenIdentifiers.uuid=tokens.uuid WHERE tokens.setCode=:setCode AND tokens.number=:number'; + $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."; + 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'], + ]; + } +} + +/* + * 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') + ->paperSize(8.27, 11.7) # A4 + ->margins(0, 0, 0, 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