Repository: dotemacs/pdfboxing
Pull request #69 (2023-03-08 20:09:22)

Pdf to html
## Description of your pull request

This PR is really https://github.com/dotemacs/pdfboxing/pull/39 but since the upstream was deleted, I'm creating it here.

/cc @StankovicMarko 


Patch:
diff --git a/README.md b/README.md
index edf9f32..eab20d3 100644
--- a/README.md
+++ b/README.md
@@ -120,11 +120,21 @@ on which the line should be drawn
                               :y1 160})
 ```
 
+### Convert a PDF document to a very simple HTML document
+
+Supply a PDF document's name, a simple HTML is created in the root folder
+
+```clojure
+(require '[pdfboxing.tools :as tools])
+(tools/pdf-to-html "myFile.pdf")
+```
+
 ## Compatibility with PDFBox's PDDocuments
 
 The following functions referenced above have direct compatibility
 with PDFBox's internal PDDocument type:
 
+>>>>>>> f3d4a82 (added pdf-to-html tool function)
 - `text/extract`
 - `pdf/split-pdf`
 - `form/get-fields`
diff --git a/deps.edn b/deps.edn
index 8aed294..679c404 100644
--- a/deps.edn
+++ b/deps.edn
@@ -1,5 +1,6 @@
 {:deps {org.clojure/clojure {:mvn/version "1.11.1"},
-        org.apache.pdfbox/pdfbox {:mvn/version "2.0.27"}}
+        org.apache.pdfbox/pdfbox {:mvn/version "2.0.27"}
+        org.apache.pdfbox/pdfbox-tools {:mvn/version "2.0.27"}}
  :aliases {:test {:extra-paths ["test"]
                   :extra-deps {org.clojure/test.check {:mvn/version "RELEASE"}}}
            :runner {:extra-deps {com.cognitect/test-runner
diff --git a/project.clj b/project.clj
index ede5a39..0a76d6a 100644
--- a/project.clj
+++ b/project.clj
@@ -4,4 +4,5 @@
   :license {:name "BSD"
             :url "http://www.opensource.org/licenses/bsd-license"}
   :dependencies [[org.clojure/clojure "1.11.1"]
-                 [org.apache.pdfbox/pdfbox "2.0.27"]])
+                 [org.apache.pdfbox/pdfbox "2.0.27"]
+                 [org.apache.pdfbox/pdfbox-tools "2.0.27"]])
diff --git a/src/pdfboxing/html.clj b/src/pdfboxing/html.clj
new file mode 100644
index 0000000..0fe115b
--- /dev/null
+++ b/src/pdfboxing/html.clj
@@ -0,0 +1,11 @@
+(ns pdfboxing.html
+  (:import (org.apache.pdfbox.tools ExtractText)))
+
+
+(defn pdf-to-html
+  "Convert a PDF document to a simple HTML file.
+  `pdf-doc` - string, name of a PDF document that is in the root folder
+  this is command line tool from pdfbox more info at:
+  https://pdfbox.apache.org/2.0/commandline.html#extracttext"
+  [pdf-doc]
+  (ExtractText/main (into-array String ["-html" pdf-doc])))


Test patch:
diff --git a/test/pdfboxing/html_test.clj b/test/pdfboxing/html_test.clj
new file mode 100644
index 0000000..34d194a
--- /dev/null
+++ b/test/pdfboxing/html_test.clj
@@ -0,0 +1,18 @@
+(ns pdfboxing.html-test
+  (:require [clojure.test :refer [deftest is]]
+            [pdfboxing.html :refer [pdf-to-html]]))
+
+(def line-separator (System/getProperty "line.separator"))
+
+(deftest pdf-to-html-tool
+  (let [_html-doc (pdf-to-html "test/pdfs/hello.pdf")]
+    (is (= (str "
+<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
+\"http://www.w3.org/TR/html4/loose.dtd\">
+<html><head><title></title>
+<meta http-equiv=\"Content-Type\" content=\"text/html; charset=\"UTF-8\">
+                </head>
+                <body>
+                <div style=\"page-break-before:always; page-break-after:always\"><div><p>Hello, this is pdfboxing.text</p>
+                </div></div>
+                </body></html>" line-separator)) (slurp "test/pdfs/hello.html"))))
diff --git a/test/pdfs/hello.html b/test/pdfs/hello.html
new file mode 100644
index 0000000..89c5385
--- /dev/null
+++ b/test/pdfs/hello.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
+"http://www.w3.org/TR/html4/loose.dtd">
+<html><head><title></title>
+<meta http-equiv="Content-Type" content="text/html; charset="UTF-8">
+</head>
+<body>
+<div style="page-break-before:always; page-break-after:always"><div><p>Hello, this is pdfboxing.text</p>
+
+</div></div>
+</body></html>
\ No newline at end of file