From 5d41d5775c7d33503d57aacbf45390608c59ec3e Mon Sep 17 00:00:00 2001
From: Vladimir Timofeenko <id@vtimofeenko.com>
Date: Sat, 24 Dec 2022 15:42:32 -0800
Subject: [PATCH] nix: move to weasypdf from wkhtmltopdf

Closes #1873
---
 nix/checks/solr.nix    |  2 --
 nix/dev-vm/default.nix |  2 +-
 nix/modules/joex.nix   | 64 ++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 63 insertions(+), 5 deletions(-)

diff --git a/nix/checks/solr.nix b/nix/checks/solr.nix
index 10150a42..f2171eab 100644
--- a/nix/checks/solr.nix
+++ b/nix/checks/solr.nix
@@ -6,8 +6,6 @@
   nixpkgs.config = {
     permittedInsecurePackages = [
       "solr-8.6.3"
-      # NOTE: Qtwebkit is a dep for wkhtmltopdf, this line is needed until #201765 is fixed in nixpkgs
-      "qtwebkit-5.212.0-alpha4"
     ];
   };
 
diff --git a/nix/dev-vm/default.nix b/nix/dev-vm/default.nix
index 6f4e93f6..01e172a1 100644
--- a/nix/dev-vm/default.nix
+++ b/nix/dev-vm/default.nix
@@ -16,7 +16,7 @@
   };
 
   # Otherwise oomkiller kills docspell
-  virtualisation.memorySize = 2048;
+  virtualisation.memorySize = 4096;
 
   virtualisation.forwardPorts = [
     # SSH
diff --git a/nix/modules/joex.nix b/nix/modules/joex.nix
index f31d25fa..7890d690 100644
--- a/nix/modules/joex.nix
+++ b/nix/modules/joex.nix
@@ -178,8 +178,24 @@ let
 
       wkhtmlpdf = {
         command = {
-          program = "${pkgs.wkhtmltopdf}/bin/wkhtmltopdf";
-          args = [ "-s" "A4" "--encoding" "UTF-8" "-" "{{outfile}}" ];
+          program = "";
+          args = [ "--encoding" "UTF-8" "-" "{{outfile}}" ];
+          timeout = "2 minutes";
+        };
+        working-dir = "/tmp/docspell-convert";
+      };
+
+      weasyprint = {
+        command = {
+          program = "${pkgs.python310Packages.weasyprint}/bin/weasyprint";
+          args = [
+            "--optimize-size"
+            "all"
+            "--encoding"
+            "{{encoding}}"
+            "-"
+            "{{outfile}}"
+          ];
           timeout = "2 minutes";
         };
         working-dir = "/tmp/docspell-convert";
@@ -1207,6 +1223,11 @@ in
                 converted to a PDF file.
               '';
             };
+            html-converter = mkOption {
+              type = types.enum [ "wkhtmlpdf" "weasyprint" ];
+              default = "weasyprint";
+              description = "Which tool to use for converting html to pdfs";
+            };
             wkhtmlpdf = mkOption {
               type = types.submodule ({
                 options = {
@@ -1246,6 +1267,45 @@ in
                 wkhtmltopdf is used.
               '';
             };
+            weasyprint = mkOption {
+              type = types.submodule ({
+                options = {
+                  working-dir = mkOption {
+                    type = types.str;
+                    default = defaults.convert.weasyprint.working-dir;
+                    description = "Directory where the conversion processes can put their temp files";
+                  };
+                  command = mkOption {
+                    type = types.submodule ({
+                      options = {
+                        program = mkOption {
+                          type = types.str;
+                          default = defaults.convert.weasyprint.command.program;
+                          description = "The path to the executable.";
+                        };
+                        args = mkOption {
+                          type = types.listOf types.str;
+                          default = defaults.convert.weasyprint.command.args;
+                          description = "The arguments to the program";
+                        };
+                        timeout = mkOption {
+                          type = types.str;
+                          default = defaults.convert.weasyprint.command.timeout;
+                          description = "The timeout when executing the command";
+                        };
+                      };
+                    });
+                    default = defaults.convert.weasyprint.command;
+                    description = "The system command";
+                  };
+                };
+              });
+              default = defaults.convert.weasyprint;
+              description = ''
+                To convert HTML files into PDF files, the external tool
+                weasyprint is used.
+              '';
+            };
             tesseract = mkOption {
               type = types.submodule ({
                 options = {