commit febd65e9808c3207f9ca4b873e6de317be23f336 Author: Skylar Ittner Date: Fri Oct 2 13:22:48 2020 -0600 Init diff --git a/README.md b/README.md new file mode 100644 index 0000000..fdad119 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +A simple script that accepts a PDF file over HTTP and prints it. + +Usage: + +* `print.php?action=list`: Returns a JSON list of available printers. + * Example response: `{"printers": ["canon-123", "brother-etc"]}` +* `print.php?action=print&printer=[printername]`: Print a PDF to the specified printer. The request body shall contain the PDF document. + * Example curl command: `curl --data-binary "@/home/user/test.pdf" "http://localhost/print.php?action=print&printer=brother-etc"` + +Requires `lp`, `lpstat`, and `pdfinfo` commands to be available. \ No newline at end of file diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..112ce7b --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,7 @@ +include.path=${php.global.include.path} +php.version=PHP_74 +source.encoding=UTF-8 +src.dir=. +tags.asp=false +tags.short=false +web.root=. diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..5af9550 --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,9 @@ + + + org.netbeans.modules.php.project + + + PDF-Web-Print-CUPS-Proxy + + + diff --git a/print.php b/print.php new file mode 100644 index 0000000..4b96c4c --- /dev/null +++ b/print.php @@ -0,0 +1,73 @@ + "lp", + "lpstat" => "lpstat", + "pdfinfo" => "pdfinfo" +]; + +header("Content-Type: application/json"); + +function getPrinters(): array { + global $CMDS; + $printerlist = `$CMDS[lpstat] -a`; + $lines = explode("\n", $printerlist); + + $printers = []; + foreach ($lines as $line) { + $printer = explode(" ", $line, 2)[0]; + if (empty($printer)) { + continue; + } + $printers[] = $printer; + } + return $printers; +} + +if ($_GET["action"] == "list") { + exit(json_encode([ + "printers" => getPrinters() + ])); +} else if ($_GET["action"] == "print") { + + if (empty($_GET["printer"]) || !in_array($_GET["printer"], getPrinters())) { + http_response_code(400); + exit(json_encode([ + "error" => "No printer specified or printer does not exist." + ])); + } + $printer = $_GET["printer"]; + + $content = file_get_contents("php://input"); + + $pdffile = tempnam(sys_get_temp_dir(), "WEB_PDF_PRINT"); + + file_put_contents($pdffile, $content); + + $pdfinfo = `$CMDS[pdfinfo] $pdffile`; + preg_match('/Page size: *([0-9]*\.?[0-9]?) x ([0-9]*\.?[0-9]?)/', $pdfinfo, $matches); + $width = $matches[1]; + $height = $matches[2]; + $media = "Custom.$width" . "x$height"; + + `$CMDS[lp] -o media=$media -d $printer $pdffile`; + + unlink($pdffile); + + exit(json_encode([ + "printed" => true + ])); +} else { + http_response_code(404); + exit(json_encode([ + "error" => "No action or invalid action specified." + ])); +} \ No newline at end of file