Printer API allows us to use locally installed printers to print documents from a web application. It starts a simple expressjs server which exposes a basic REST API.
Download the latest release and install it on your local machine.
Get the current state:
curl localhost:1761/status
Get all printers:
curl localhost:1761/printers
Upload a PDF to the api. This requires the document
field to be a PDF:
curl http://localhost:1761/print\?printer\=PRINTERNAME -F "[email protected]"
Print the pdf from a url:
curl -X POST http://localhost:1761/print\?printer\=PRINTERNAME\&url\=http://pdf.com
In our Javascript example we assume you can download a pdf from a specific location in your application. We use javascript, as this (probably) allows us to reuse an existing session should the pdf require authentication.
<script>
function printPdf(url, printer) {
fetch(url, {method: 'GET'})
.then(response => response.blob())
.then((blob) => {
const data = new FormData()
data.append('document', blob)
fetch(`http://localhost:1761/print?printer=${printer}`, {
method: 'POST',
body: data,
})
})
}
</script>
Sorry, this code doesn't come with tests yet.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Coppertrack does not provide commercial support or paid development.