summary refs log tree commit diff
path: root/pkgs/tools/text/ocrmypdf/default.nix
blob: 13353daa1711945aef682883c47e9a8847448fd3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{ fetchFromGitHub
, ghostscript
, img2pdf
, jbig2enc
, leptonica
, pngquant
, python3
, python3Packages
, qpdf
, stdenv
, tesseract4
, unpaper
}:

let
  inherit (python3Packages) buildPythonApplication;

  runtimeDeps = with python3Packages; [
    ghostscript
    jbig2enc
    leptonica
    pngquant
    qpdf
    tesseract4
    unpaper
    pillow
  ];

in buildPythonApplication rec {
  pname = "ocrmypdf";
  version = "9.2.0";
  disabled = ! python3Packages.isPy3k;

  src = fetchFromGitHub {
    owner = "jbarlow83";
    repo = "OCRmyPDF";
    rev = "v${version}";
    sha256 = "1mvc6x5nn242z65pxv39ch71vaikgi89bb0sjbfy2jbw91vk41xa";
  };

  nativeBuildInputs = with python3Packages; [
    pytestrunner
    setuptools
    setuptools-scm-git-archive
    setuptools_scm
  ];

  propagatedBuildInputs = with python3Packages; [
    cffi
    chardet
    img2pdf
    pdfminer
    pikepdf
    pillow
    reportlab
    setuptools
    tqdm
  ];

  checkInputs = with python3Packages; [
    pypdf2
    pytest
    pytest-helpers-namespace
    pytest_xdist
    pytestcov
    pytestrunner
    python-xmp-toolkit
    setuptools
  ] ++ runtimeDeps;

  postPatch = ''
    substituteInPlace src/ocrmypdf/leptonica.py \
      --replace "lept = ffi.dlopen(_libpath)" \
      'lept = ffi.dlopen("${stdenv.lib.makeLibraryPath [leptonica]}/liblept${stdenv.hostPlatform.extensions.sharedLibrary}")'
  '';

  # The tests take potentially 20+ minutes, depending on machine
  doCheck = false;

  # These tests fail and it might be upstream problem... or packaging. :)
  # development is happening on macos and the pinned test versions are
  # significantly newer than nixpkgs has. Program still works...
  # (to the extent I've used it) -- Kiwi
  checkPhase = ''
    export HOME=$TMPDIR
    pytest -k 'not test_force_ocr_on_pdf_with_no_images \
    and not test_tesseract_crash \
    and not test_tesseract_crash_autorotate \
    and not test_ghostscript_pdfa_failure \
    and not test_gs_render_failure \
    and not test_gs_raster_failure \
    and not test_bad_utf8 \
    and not test_old_unpaper'
  '';

  makeWrapperArgs = [ "--prefix PATH : ${stdenv.lib.makeBinPath [ ghostscript jbig2enc pngquant qpdf tesseract4 unpaper ]}" ];

  meta = with stdenv.lib; {
    homepage = "https://github.com/jbarlow83/OCRmyPDF";
    description = "Adds an OCR text layer to scanned PDF files, allowing them to be searched";
    license = licenses.gpl3;
    platforms = platforms.linux;
    maintainers = [ maintainers.kiwi ];
  };
}