Skip to content

Commit

Permalink
Merge pull request #71 from sampoto/pdfa-conformance-conf
Browse files Browse the repository at this point in the history
Add configuration for PDF/A conformance level
  • Loading branch information
erno authored Aug 31, 2023
2 parents 3dcfd5a + 4384133 commit 9c15dcd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
13 changes: 12 additions & 1 deletion programs/pdf2pdfa
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ OUTPUT=$2
DPI=$3
MAXBITMAP=$4
PDFSETTINGS=$5
PDFA_CONFORMANCE=$6

if [ -z "$DPI" ]; then
DPI=720
Expand Down Expand Up @@ -43,6 +44,16 @@ if ! [[ $MAXBITMAP =~ $re ]] ; then
exit 2
fi

PDFA_CONFORMANCE_NUM=""
if [[ ! $PDFA_CONFORMANCE =~ $re ]] || [[ "$PDFA_CONFORMANCE" -lt 1 ]] || [[ "$PDFA_CONFORMANCE" -gt 3 ]]; then
echo "error: PDFA_CONFORMANCE invalid - only values from 1 to 3 are allowed"
exit 2
fi
if [ -n "$PDFA_CONFORMANCE" ] && [ "$PDFA_CONFORMANCE" -gt 1 ]; then
# The ghostscript documentation specifies that -dPDFA parameter should only have the version value followed when using 2 or 3
PDFA_CONFORMANCE_NUM="=$PDFA_CONFORMANCE"
fi

docker run \
--runtime="${LAUNDRY_DOCKER_RUNTIME:-runsc}" \
--network=none \
Expand All @@ -53,5 +64,5 @@ docker run \
--rm \
laundry-programs \
/bin/bash -c 'cat > /home/docconv/document.pdf && \
gs -q -dPDFA -dBATCH -dNOPAUSE -r'$DPI' -dMaxBitmap='$MAXBITMAP' '$PDFSETTINGS' -sProcessColorModel=DeviceCMYK -sDEVICE=pdfwrite -dPDFACompatibilityPolicy=1 -sOutputFile=- /home/docconv/document.pdf' \
gs -q -dPDFA'$PDFA_CONFORMANCE_NUM' -dBATCH -dNOPAUSE -r'$DPI' -dMaxBitmap='$MAXBITMAP' '$PDFSETTINGS' -sProcessColorModel=DeviceCMYK -sDEVICE=pdfwrite -dPDFACompatibilityPolicy=1 -sOutputFile=- /home/docconv/document.pdf' \
< "$INPUT" > "$OUTPUT"
6 changes: 5 additions & 1 deletion resources/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
var pdfParameters = [
{"type": "pdf/pdf2pdfa", key: "dpi", dtype: "number", wrapper: "pdf_dpi_elem", elem: "pdf_dpi" },
{"type": "pdf/pdf2pdfa", key: "maxbitmap", dtype: "number", wrapper: "pdf_maxbitmap_elem", elem: "pdf_maxbitmap" },
{"type": "pdf/pdf2pdfa", key: "pdfsettings", wrapper: "pdf_pdfsettings_elem", elem: "pdf_pdfsettings" }
{"type": "pdf/pdf2pdfa", key: "pdfsettings", wrapper: "pdf_pdfsettings_elem", elem: "pdf_pdfsettings" },
{"type": "pdf/pdf2pdfa", key: "pdfaconformance", dtype: "number", wrapper: "pdf_pdfaconformance_elem", elem: "pdf_pdfaconformance" }
]

function updateConversionParameters() {
Expand Down Expand Up @@ -104,6 +105,9 @@ <h1>Laundry</h1>
<div id="pdf_pdfsettings_elem">
<input type="text" id="pdf_pdfsettings" name="pdfsettings" value="/default"> Pdfsettings
</div>
<div id="pdf_pdfaconformance_elem">
<input type="number" id="pdf_pdfaconformance" name="pdfaconformance" value="1"> Pdf/a conformance (Valid values: 1-3)
</div>
<form id="pdfop">
<input type="file" name="file">
<input type="button" value="Convert" onclick="pdfConverter()">
Expand Down
39 changes: 23 additions & 16 deletions src/laundry/pdf.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,28 @@
(proxy-super close)
(io/delete-file path)))))

(s/defn validate-pdf-settings [pdfsettings :- s/Str]
(when-not (#{"/screen" "/ebook" "/printer" "/prepress" "/default"} pdfsettings) "pdfsettings is not given in correct format"))

;; pdf/a converter
(s/defn api-pdf2pdfa [env, tempfile :- java.io.File, dpinum :- s/Int, maxbitmapnum :- s/Int, pdfsettings :- s/Str]
(let [in-path (.getAbsolutePath tempfile)
out-path (str (.getAbsolutePath tempfile) ".pdf")
dpi (str dpinum)
maxbitmap (str maxbitmapnum)
pdfsettings (str pdfsettings)
res (shell-out! (str (:tools env) "/pdf2pdfa")
in-path out-path dpi maxbitmap pdfsettings)]
(.delete tempfile)
(if (= (:exit res) 0)
(htresp/content-type
(htresp/ok (temp-file-input-stream out-path))
"application/pdf")
(badness-resp "pdf2pdfa conversion failed" res))))
(s/defn api-pdf2pdfa [env, tempfile :- java.io.File, dpinum :- s/Int, maxbitmapnum :- s/Int, pdfsettings :- s/Str, pdfaconformancenum :- s/Int]
(if-let [pdfsettings-error (validate-pdf-settings (str pdfsettings))]
(badness-resp pdfsettings-error pdfsettings-error)

(let [in-path (.getAbsolutePath tempfile)
out-path (str (.getAbsolutePath tempfile) ".pdf")
dpi (str dpinum)
maxbitmap (str maxbitmapnum)
pdfsettings (str pdfsettings)
pdfaconformance (str pdfaconformancenum)
res (shell-out! (str (:tools env) "/pdf2pdfa")
in-path out-path dpi maxbitmap pdfsettings pdfaconformance)]
(.delete tempfile)
(if (= (:exit res) 0)
(htresp/content-type
(htresp/ok (temp-file-input-stream out-path))
"application/pdf")
(badness-resp "pdf2pdfa conversion failed" res)))))

;; pdf → txt conversion
(s/defn api-pdf2txt [env, tempfile :- java.io.File]
Expand Down Expand Up @@ -80,11 +87,11 @@
(api-pdf2txt env tempfile)))
(POST "/pdf2pdfa" []
:summary "attempt to convert a PDF file to PDF/A"
:query-params #_{:clj-kondo/ignore [:unresolved-symbol]} [{dpi :- s/Int 720} {maxbitmap :- s/Int 0} {pdfsettings :- s/Str "/default"}]
:query-params #_{:clj-kondo/ignore [:unresolved-symbol]} [{dpi :- s/Int 720} {maxbitmap :- s/Int 0} {pdfsettings :- s/Str "/default"} {pdfaconformance :- s/Int 1}]
:multipart-params [file :- upload/TempFileUpload]
:middleware [wrap-multipart-params]
(let [tempfile (:tempfile file)
filename (:filename file)]
(info "PDF converter received " filename "(" (:size file) "b)")
(.deleteOnExit tempfile) ;; cleanup if VM is terminated
(api-pdf2pdfa env tempfile dpi maxbitmap pdfsettings))))))
(api-pdf2pdfa env tempfile dpi maxbitmap pdfsettings pdfaconformance))))))
1 change: 1 addition & 0 deletions test/laundry/pdf_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
(assoc-in [:query-params :dpi] 720)
(assoc-in [:query-params :maxbitmap] 0)
(assoc-in [:query-params :pdfsettings] "/default")
(assoc-in [:query-params :pdfaconformance] 2)
(merge (peridot.multipart/build {:file file})))
response (app request)
body (ring.util.request/body-string response)]
Expand Down

0 comments on commit 9c15dcd

Please sign in to comment.