summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2023-11-25 20:11:27 +0100
committerDaniel Weipert <code@drogueronin.de>2023-11-25 20:11:27 +0100
commita65b12797c73a4b0be6f2e73a805d476a1581e95 (patch)
tree4f8fbde882917941bba45d689b17e8d533eb190e
initial commit
-rw-r--r--.gitignore4
-rw-r--r--Justfile2
-rw-r--r--composer.json23
-rw-r--r--docker-compose.yml16
-rw-r--r--index.php101
5 files changed, 146 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..a4ea68a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+/vendor/
+/output/
+
+AllPrintings.sqlite
diff --git a/Justfile b/Justfile
new file mode 100644
index 0000000..b5bb857
--- /dev/null
+++ b/Justfile
@@ -0,0 +1,2 @@
+generate deck:
+ DECKLIST="{{deck}}" docker compose run php
diff --git a/composer.json b/composer.json
new file mode 100644
index 0000000..1f6c131
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,23 @@
+{
+ "name": "dweipert/mtg-pdf",
+ "authors": [
+ {
+ "name": "Daniel Weipert",
+ "email": "code@drogueronin.de"
+ }
+ ],
+ "require": {
+ "gotenberg/gotenberg-php": "^1.1",
+ "guzzlehttp/guzzle": "^7.8"
+ },
+ "autoload": {
+ "psr-4": {
+ "Dweipert\\MtgPdf\\": "src/"
+ }
+ },
+ "config": {
+ "allow-plugins": {
+ "php-http/discovery": true
+ }
+ }
+}
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..f2b177c
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,16 @@
+version: "3"
+
+services:
+ gotenberg:
+ image: "gotenberg/gotenberg:7"
+
+ php:
+ image: "php"
+ links:
+ - "gotenberg"
+ environment:
+ - "DECKLIST=${DECKLIST}"
+ volumes:
+ - "./:/usr/src/app"
+ working_dir: "/usr/src/app"
+ command: ["php", "./index.php"]
diff --git a/index.php b/index.php
new file mode 100644
index 0000000..3132381
--- /dev/null
+++ b/index.php
@@ -0,0 +1,101 @@
+<?php
+
+use Gotenberg\Gotenberg;
+use Gotenberg\Stream;
+
+require_once __DIR__ . '/vendor/autoload.php';
+
+$decklist = $_ENV['DECKLIST'];
+
+/*
+ * (\d+) ([\w\s]+) \(([\w\d]+)\) (\d+)
+ * 2 Hall of Storm Giants (AFR) 257
+ */
+
+preg_match_all("/(\d+) ([\w\s]+) \(([\w\d]+)\) (\d+)/", $decklist, $matches);
+$cards = [];
+for ($idx = 0; $idx < count($matches[0]); $idx++) {
+ $cards[] = [
+ 'quantity' => $matches[1][$idx],
+ 'name' => $matches[2][$idx],
+ 'set' => $matches[3][$idx],
+ 'number' => $matches[4][$idx],
+ ];
+}
+
+/*
+ * SELECT scryfallId from cardIdentifiers
+ * JOIN cards ON cardIdentifiers.uuid=cards.uuid
+ * WHERE cards.setCode='$SETCODE' AND cards.number='$NUMBER';
+ */
+
+$db = new \PDO('sqlite:AllPrintings.sqlite');
+
+$images = [];
+foreach ($cards as $card) {
+ $query = 'SELECT scryfallId 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']]);
+
+ $id = $statement->fetchColumn()?: '';
+
+ if (empty($id)) {
+ continue;
+ }
+
+ // TODO: check double sided
+
+ $images[] = [
+ 'quantity' => $card['quantity'],
+ 'src' => "https://cards.scryfall.io/png/front/" . substr($id, 0, 1) . "/" . substr($id, 1, 1) . "/$id.png",
+ ];
+}
+// https://cards.scryfall.io/png/front/substr($scryfallId, 0, 1)/substr($scryfallId, 1, 1)/$scryfallId.png
+
+/*
+ curl \
+--request POST 'http://localhost:3000/forms/chromium/convert/html' \
+--form 'files=@"./index.html"' \
+-o my.pdf -F marginTop=0.0 -F marginBottom=0.0 -F marginRight=0.0 -F marginLeft=0.0
+ */
+
+$template = <<<HTML
+<html>
+ <head>
+ <style>* { margin: 0; padding: 0; } html { } #bg { background-color: #fff; display: flex; flex-wrap: wrap; } img {display:block;}</style>
+ <style>
+ @media print {
+ @page {
+ }
+ }
+ html, body {
+ padding: 0;
+ margin: 0;
+ }
+ img {
+ width: 2.49in;
+ height: 3.48in;
+ background-color: #16130e;
+ background-color: #000;
+ }
+ </style>
+ </head>
+ <body>
+ <div id="bg">{{imgs}}</div>
+ </body>
+</html>
+HTML;
+
+$imgs = array_map(function ($image) {
+ return str_repeat("<img src=\"$image[src]\">", $image['quantity']);
+}, $images);
+
+$html = str_replace('{{imgs}}', implode('', $imgs), $template);
+
+$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));
+
+Gotenberg::save($request, __DIR__ . '/output');