glabels-3-batch to /dev/stdout to LPR Printer

by | Jan 27, 2020 | IT Tips | 0 comments

Unfortunately the following doesn't work because the informational output gets mixed with the PDF output. As you can see here Object w = etc

I have been doing a two step glabels-3-batch to LPR process but have made it so I don't have to write an intermediate file to disk.

Here is my approach in shell scripting

1
2
cat merge.csv | glabels-3-batch -i - -o /dev/stdout template.glabels | \
sed -n '/%PDF-1.5/,/%%EOF/p' | lpr -PPDF -J jobname

And in PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
    * Send job to gLabels
    *
    * Sends the completed template to the printer held in the $print_settings array
    * @param string $template full path to glabels template
    * @param array $printerDetails Printer Information
    * @return array Array holding the results of the lpr command
    */
   public function glabelsBatchPrint(string $template, $printerDetails)
   {
       $this->setGlabelsTemplate($template);
 
       $this->setPdfOutFile(); // this is /dev/stdout
        
       // this writes the glabels csv data to disk
       // for testing
       $this->createTempFile($this->printContent);
 
       $cmdArgs = [
           'glabels-3-batch',
           '-o',
           $this->getPdfOutFile(), // use tempnam(TMP, glabels) to get a file name
           $this->getGlabelsTemplate(),
       ];
 
       // when you have csv data your need to merge it into the template
       // from stdin eg. cat merge.csv | glabels-3-batch -i - -o outfile.pdf template.glabels
       if ($this->glabelsMergeCSV) {
           /**
            * add stdin ("-i -") to glabels command line when piping  CSV data into glabels:
            * glabels-3-batch -i - -o
            *      /var/www/wms/app/tmp/20190701182321-customPrint.pdf
            *      /var/www/wms/app/webroot/files/templates/100x50custom.glabels
            */
           array_splice($cmdArgs, 1, 0, ['-i', '-']);
       }
       // printContent is set elsewhere but contains
       // the merge csv data
       $results = $this->runProcess(
           implode(' ', $cmdArgs),
           $this->printContent
       );
 
       // bug out if the glabel-3-batch process fails
       if ($results['return_value'] !== 0) {
           return $results;
       }
        
       // if successful pull the stdout which was returned from
       // glabels-3-batch and
       // extract the PDF from the stdout stream
       //$pdfPattern = '/(%PDF-1.5.*%%EOF)/s';
 
       //preg_match($pdfPattern, $results['stdout'], $matches);
 
       /**
        * This grabs the PDF file out of the PDF
        */
       //$this->setPrintContent($matches[0]);
       $this->setPrintContent(file_get_contents($this->getPdfOutFile()));
 
       unlink($this->getPdfOutFile());
 
       return $this->sendPdfToLpr($printerDetails);
   }
 
   /**
    * send gLabels PDF to designated LPR printer
    * @param string $printer Print queue name
    * @return array Array with stdout stderr cmd and return_value
    */
   public function sendPdfToLpr($printer)
   {
       $jobId = $this->getJobId();
 
       $cmdArgs = [
           'lpr',
           '-P',
           $printer,
           '-#',
           $this->glabelPrintCopies,
           '-J',
           $jobId,
           '-T',
           $jobId,
       ];
 
       $returnValue = $this->runProcess(
           implode(' ', $cmdArgs),
           $this->printContent
       );
 
       // set back to 0 for next run
       $this->setPrintCopies(0);
 
       return $returnValue;
   }
 
   /**
    * run process
    * @param string $cmd Command line to run
    * @param array $printContent Not sure what this is
    * @return array array of stdin, out,err and exit code, cmd
    */
   public function runProcess($cmd, $printContent)
   {
       // code...
       $descriptorspec = [
           0 => ['pipe', 'r'], // stdin
           1 => ['pipe', 'w'], // stdout
           2 => ['pipe', 'w'], // stderr
       ];
 
       $pipes = [];
 
       $process = proc_open(
           $cmd,
           $descriptorspec,
           $pipes,
           $this->getCwd(), //cwd orig TMP
           null// env null = current
       );
 
       // writing straight to stdin works
       fwrite($pipes[0], $printContent);
       fclose($pipes[0]);
 
       $stdout = stream_get_contents($pipes[1]);
       fclose($pipes[1]);
 
       $stderr = stream_get_contents($pipes[2]);
       fclose($pipes[2]);
 
       $return_value = proc_close($process);
 
       /* return an array with all the necessary information */
       return compact('cmd', 'stdout', 'stderr', 'return_value');
   }

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.