changeset 915:b47f64276427

Try to compress PDF if gs is available
author HIROSE Yuuji <yuuji@gentei.org>
date Fri, 08 Jan 2021 19:01:50 +0900
parents cfb6247a2266
children 7cac220d52a7
files s4-funcs.sh s4-main.js
diffstat 2 files changed, 38 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/s4-funcs.sh	Wed Jan 06 12:56:33 2021 +0900
+++ b/s4-funcs.sh	Fri Jan 08 19:01:50 2021 +0900
@@ -1055,6 +1055,12 @@
   fi
   $sha1 "$@" | cut -d' ' -f1
 }
+if type gs >/dev/null 2>&1; then
+  gs_pdfwrite() {
+    gs -sDEVICE=pdfwrite -dPDFSETTINGS=/default \
+       -dNOPAUSE -dQUIET -dBATCH -o "$2" "$1"
+  }
+fi
 enjpeg() {
   if [ -z "$cjpeg" ]; then
     if type cjpeg >/dev/null 2>&1; then
@@ -1580,6 +1586,16 @@
 		mogrify -quality 75 -resize $maximagexy'>' "$tmpd/$v"
 		err "Mogrified: `ls -lF $tmpd/$v`"	# 2020-05-31
 		;;
+	      [Aa]pplication/[Pp][Dd][Ff])
+		if [ x"`getpar comppdf`" = x"yes" ]; then
+		  if type gs_pdfwrite >/dev/null 2>&1; then
+		    err Calling gs
+		    gs_pdfwrite "$tmpd/$v" "$tmpd/$v.mini.pdf" && {
+		      err "PDF compressed: `ls -lF $tmpd/${v}*`"
+		      mv "$tmpd/$v.mini.pdf" "$tmpd/$v"
+		    }
+		  fi
+		fi
 	    esac
 	    if ! echo "$mimetype" | egrep "$file_accept_egrep" >/dev/null 2>&1
 	    then
--- a/s4-main.js	Wed Jan 06 12:56:33 2021 +0900
+++ b/s4-main.js	Fri Jan 08 19:01:50 2021 +0900
@@ -6,6 +6,7 @@
     var myurl = document.URL,
 	mypath = myurl.substring(myurl.lastIndexOf("/"));
     var art_m_list = [];
+    let input_pdfsw = 'input[name="comppdf"]';
     if (mypath.match(/(.*)\/(.*)/)) {
 	mypath = RegExp.$2;
 	mypath = mypath.substring(0, mypath.lastIndexOf("?"));
@@ -189,16 +190,31 @@
 	szmax = parseInt(szmax);
 	if (szmax <= 0) return;
 	// szmax = 10000
-	let ng = "", rcval=false, fileexists=false;
+	let ng = "", rcval=false, fileexists=false,
+	    pdfsw = form.querySelector(input_pdfsw),
+	    pdfmsg = "Try compressing PDF?\nPDFを圧縮してみますか?\n" +
+	    	     "(それでも収まらない場合もあります)";
 	for (let f of form.querySelectorAll('input[type="file"]')) {
-	    let thiserr = false
+	    let thiserr = false;
 	    for (let i of f.files) {
 		fileexists = true;
 		let fn = i.name, sz = i.size;
 		console.log("max="+szmax+", fn="+fn+", sz="+sz);
 		if (sz > szmax) {
-		    thiserr = true;
-		    ng += ((ng>"" ? ", " : "")+fn)
+		    if (fn.match(/\.pdf/i)
+			&& sz < szmax*3	// XXX : x3 reasonable?
+			&& (pdfsw || confirm(pdfmsg))) {
+			if (!pdfsw) {
+			    pdfsw = document.createElement("input");
+			    pdfsw.name = "comppdf";
+			    pdfsw.type = "text";
+			    f.parentNode.insertBefore(pdfsw, f);
+			    pdfsw.value = "yes";
+			}
+		    } else {
+			thiserr = true;
+			ng += ((ng>"" ? ", " : "")+fn)
+		    }
 		}
 	    }
 	    thiserr ? f.classList.add("warnbg") : f.classList.remove("warnbg");
@@ -267,6 +283,8 @@
 	    newform = new FormData(form);
 	    if (data.get("text") > "") {	// Called by submit button
 		myform.reset();
+		let pdfsw = myform.querySelector(input_pdfsw);
+		if (pdfsw) pdfsw.remove();
 		// myform.text.value = '';
 	    }
 	    myform.fetchtime.value = newform.get("fetchtime");

yatex.org