node.jsnetwork-printersnode-pdfkit

How to print pdf in nodejs


I'm using pdfkit and ipp library for printing a pdf to a network printer (Brother HL-L2360D series).

Here is the code

const ipp = require('ipp');
const concat = require('concat-stream');
const PDFDocument = require('pdfkit');

const doc = new PDFDocument({ margin: 0 });
doc.text('Testing Printing Functionality. Sent By Developer', 0, 780);

doc.pipe(concat(function (pdf) {
    const printer = ipp.Printer('http://printer.sandbox.com/ipp/print');
    const msg = {
        'operation-attributes-tag': {
            'requesting-user-name': 'William',
            'job-name': 'My Test Job',
            'document-format': 'application/octet-stream'
        },
        data: pdf
    };
    printer.execute('Print-Job', msg, function (err, res) {
        console.log({ err, res });
    });
}));
doc.end();

But it is printing some random characters

image

When I checked the printer configuration by sending Get-Printer-Attributes operation. It returns following list of valid document supported. I see it doesn't have application/pdf. For this reason I'm sending application/octet-stream as document-format.

{
    "version": "2.0",
    "statusCode": "successful-ok",
    "id": 31208186,
    "operation-attributes-tag": {
        "attributes-charset": "utf-8",
        "attributes-natural-language": "en-us"
    },
    "printer-attributes-tag": {
        "copies-default": 1,
        "finishings-default": "none",
        "printer-info": "en\u001eBrother HL-L2360D series",
        "printer-make-and-model": "en\u001eBrother HL-L2360D series",
        "ipp-versions-supported": [
            "1.0",
            "1.1",
            "2.0"
        ],
        "operations-supported": [
            "Print-Job",
            "Validate-Job",
            "Create-Job",
            "Send-Document",
            "Cancel-Job",
            "Get-Job-Attributes",
            "Get-Jobs",
            "Get-Printer-Attributes",
            "Identify-Printer"
        ],
        "multiple-document-jobs-supported": false,
        "multiple-operation-time-out": 150,
        "natural-language-configured": "en",
        "charset-configured": "utf-8",
        "charset-supported": "utf-8",
        "document-format-supported": [
            "application/octet-stream",
            "image/urf",
            "image/pwg-raster"
        ],
        "document-format-default": "application/octet-stream",
        "printer-is-accepting-jobs": false,
        "queued-job-count": 1,
        "pdl-override-supported": "attempted",
        "printer-up-time": 14285,
        "compression-supported": "none",
        "color-supported": false,
        "landscape-orientation-requested-preferred": 5,
        "marker-colors": "en\u001e#000000",
        "marker-high-levels": 100,
        "marker-levels": 100,
        "marker-low-levels": 10,
        "marker-names": "en\u001eBK",
        "marker-types": "toner",
        "media-bottom-margin-supported": 432,
        "media-left-margin-supported": 432,
        "media-right-margin-supported": 432,
        "media-source-supported": [
            "auto",
            "manual",
            "tray-1"
        ],
        "media-top-margin-supported": 432,
        "media-type-supported": [
            "stationery",
            "stationery-lightweight",
            "stationery-heavyweight",
            "stationery-cover",
            "envelope",
            "envelope-heavyweight",
            "envelope-lightweight",
            "stationery-recycled",
            "labels",
            "stationery-bond"
        ]
    }
}

I'm not sure where I'm going wrong. Can someone please help me on this?

I tried changing the document-format. Tried creating a job first and then executing it. I also tried using iip://printer.sandbox.com/ipp/print, issue still remained same.


Solution

  • "Printer Manufacturers", will usually have a range of printers for different parts of their market. In turn those ranges may be targeting different operating systems and servers.

    Generally "Business Printers" will be using PostScript or Printer Control Language and less commonly a more problematic onboard PDF computing engine.

    The Brother model in OP question shows in the manual it is using PCL not PS

    In order to translate a PDF into PCL it will need to parse through a converter, which can be an application such as Acrobat PDF reader with PCL print driver, or perhaps directly through GhostScript/GhostPDL (neither free for commercial use).