ch25s03.html 23.2 KB
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Creating barcodes</title><link rel="stylesheet" type="text/css" href="manual.css"><meta name="generator" content="DocBook XSL Stylesheets V1.76.0"><link rel="home" href="index.html" title="JpGraph Manual"><link rel="up" href="ch25.html" title="Chapter 25. PDF417 (2D-Barcode)"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Creating barcodes</th></tr><tr><td width="20%" align="left"> </td><th width="60%" align="center">Chapter 25. PDF417 (2D-Barcode)</th><td width="20%" align="right"> </td></tr></table><hr></div><div class="sect1" title="Creating barcodes"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2600311"></a>Creating barcodes</h2></div></div></div>
        
        <p>In order to access the PDF417 functionality the module
                "<code class="filename">pdf417/jpgraph_pdf417.php</code>" must first be included.</p>
        <p>Usage of PDF417 barcodes follows a similar schema as for the linear barcodes with the
            concepts of an encoder and backend. The principle of the overall encodation process is
            shown in <a class="xref" href="ch25s03.html#fig.pdf417-encodation-overview" title="Figure 25.4. Overview of the interaction between encoder and backends">Figure 25.4. Overview of the interaction between encoder and backends</a></p>
        <p>
            </p><div class="figure"><a name="fig.pdf417-encodation-overview"></a><p class="title"><b>Figure 25.4. Overview of the interaction between encoder and backends</b></p><div class="figure-contents">
                
                <div class="mediaobject"><img src="images/pdf417-encodation-principle.png" alt="Overview of the interaction between encoder and backends"></div>
            </div></div><p><br class="figure-break">
        </p>
        <p>In order to create a PDf417 barcode the following principle steps are needed</p>
        <p>
            </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
                    <p>Create an instance of the PDF417 encoder (as an instance of <code class="code">class
                            PDF417Barcode</code> )</p>
                </li><li class="listitem">
                    <p>Create an instance of a backend to produce the wanted output (image,
                        postscript or encapsulated postscript) by using the static factory method
                            <code class="code">PDF417BackendFactory::Create()</code></p>
                </li><li class="listitem">
                    <p>Encode data and send it back to the browser or a file with a call to the
                        backend <code class="code">Stroke()</code> method.</p>
                </li></ol></div><p>
        </p>
        <p>The following script shows how to create the simplest possible barcode (in PNG format)
            representing the data string "PDF-417" encoded using default number of columns and the
            default error correction level </p>
        <p>
            </p><div class="example"><a name="example.pdf417_ex0"></a><p class="title"><b>Example 25.1. The most basic PDF417 script (<code class="filename">pdf417_ex0.php</code>) </b></p><div class="example-contents">  <div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags">&lt;?php</span><span class="hl-code"> </span><span class="hl-comment">//</span><span class="hl-comment"> content=&quot;text/plain; charset=utf-8&quot;</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">require_once</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">jpgraph/pdf417/jpgraph_pdf417.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
 
</span><span class="hl-var">$data</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">PDF-417</span><span class="hl-quotes">'</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> Create a new encoder and backend to generate PNG images</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-var">$backend</span><span class="hl-code"> = </span><span class="hl-identifier">PDF417BackendFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-identifier">BACKEND_IMAGE</span><span class="hl-code">,</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">PDF417Barcode</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$backend</span><span class="hl-code">-&gt;</span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-var">$data</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></td></tr></table></div></div></div><p><br class="example-break">  </p><div class="figure"><a name="fig.pdf417_ex0"></a><p class="title"><b>Figure 25.5. The most basic PDF417 script <code class="uri"><a class="uri" href="example_src/pdf417_ex0.html" target="_top">(<code class="filename">pdf417_ex0.php</code>)</a></code> </b></p><div class="figure-contents">  <span class="inlinemediaobject"><img src="images/pdf417_ex0.png" alt="The most basic PDF417 script (pdf417_ex0.php)"></span> </div></div><p><br class="figure-break">
        </p>
        <p>As can bee seen from the code above the basic interface to the library makes use of
            one abstract factory to create the appropriate output backend. This design makes the
            addition of new output formats transparent for the end user of the library.</p>
        <p>The example above does not have any error handling. If there is some error in the
            process an exception will be thrown in the same way as in other places in the library.
            The default exception will display the standard library image error box. If we instead
            always wanted to do some additional processing and perhaps just display a text based
            re-formatted error message we could change the above code to catch this exception as the
            following example shows</p>
        <p>
            </p><div class="example"><a name="example.pdf417_ex1"></a><p class="title"><b>Example 25.2. Adding error handling (<code class="filename">pdf417_ex1.php</code>) </b></p><div class="example-contents">  <div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags">&lt;?php</span><span class="hl-code"> </span><span class="hl-comment">//</span><span class="hl-comment"> content=&quot;text/plain; charset=utf-8&quot;</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">require_once</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">jpgraph/pdf417/jpgraph_pdf417.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
 
</span><span class="hl-var">$data</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">PDF-417</span><span class="hl-quotes">'</span><span class="hl-code">;
</span><span class="hl-reserved">try</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-comment">//</span><span class="hl-comment"> Create a new encoder and backend to generate PNG images</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-var">$backend</span><span class="hl-code"> = </span><span class="hl-identifier">PDF417BackendFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-identifier">BACKEND_IMAGE</span><span class="hl-code">,</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">PDF417Barcode</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-var">$backend</span><span class="hl-code">-&gt;</span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-var">$data</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-reserved">catch</span><span class="hl-brackets">(</span><span class="hl-identifier">JpGraphException</span><span class="hl-code"> </span><span class="hl-var">$e</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">echo</span><span class="hl-code"> </span><span class="hl-quotes">'</span><span class="hl-string">PDF417 Error: </span><span class="hl-quotes">'</span><span class="hl-code">.</span><span class="hl-var">$e</span><span class="hl-code">-&gt;</span><span class="hl-identifier">GetMessage</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-inlinetags">?&gt;</span></pre></td></tr></table></div></div></div><p><br class="example-break">  </p><div class="figure"><a name="fig.pdf417_ex1"></a><p class="title"><b>Figure 25.6. Adding error handling <code class="uri"><a class="uri" href="example_src/pdf417_ex1.html" target="_top">(<code class="filename">pdf417_ex1.php</code>)</a></code> </b></p><div class="figure-contents">  <span class="inlinemediaobject"><img src="images/pdf417_ex1.png" alt="Adding error handling (pdf417_ex1.php)"></span> </div></div><p><br class="figure-break">
        </p>
        <p>when an exception is thrown it will be caught and the error string echoed. If instead
            we wanted to specifically encode the data using 8-columns and using error detection
            level 5 the code would have to be modified to </p>
        <p>
            </p><div class="example"><a name="example.pdf417_ex1b"></a><p class="title"><b>Example 25.3. Adjusting the number of columns and error correction level (<code class="filename">pdf417_ex1b.php</code>) </b></p><div class="example-contents">  <div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags">&lt;?php</span><span class="hl-code"> </span><span class="hl-comment">//</span><span class="hl-comment"> content=&quot;text/plain; charset=utf-8&quot;</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">require_once</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">jpgraph/pdf417/jpgraph_pdf417.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
 
</span><span class="hl-var">$data</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">PDF-417</span><span class="hl-quotes">'</span><span class="hl-code">;
 
</span><span class="hl-comment">//</span><span class="hl-comment"> Setup some symbolic names for barcode specification</span><span class="hl-comment"></span><span class="hl-code">
 
</span><span class="hl-var">$columns</span><span class="hl-code"> = </span><span class="hl-number">8</span><span class="hl-code">;   </span><span class="hl-comment">//</span><span class="hl-comment"> Use 8 data (payload) columns</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-var">$errlevel</span><span class="hl-code"> = </span><span class="hl-number">2</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment"> Use error level 2 (minimum recommended)</span><span class="hl-comment"></span><span class="hl-code">
 
</span><span class="hl-comment">//</span><span class="hl-comment"> Create a new encoder and backend to generate PNG images</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">try</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-var">$encoder</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">PDF417Barcode</span><span class="hl-brackets">(</span><span class="hl-var">$columns</span><span class="hl-code">,</span><span class="hl-var">$errlevel</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-var">$backend</span><span class="hl-code"> = </span><span class="hl-identifier">PDF417BackendFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-identifier">BACKEND_IMAGE</span><span class="hl-code">,</span><span class="hl-var">$encoder</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-var">$backend</span><span class="hl-code">-&gt;</span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-var">$data</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-reserved">catch</span><span class="hl-brackets">(</span><span class="hl-identifier">JpGraphException</span><span class="hl-code"> </span><span class="hl-var">$e</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">echo</span><span class="hl-code"> </span><span class="hl-quotes">'</span><span class="hl-string">PDF417 Error: </span><span class="hl-quotes">'</span><span class="hl-code">.</span><span class="hl-var">$e</span><span class="hl-code">-&gt;</span><span class="hl-identifier">GetMessage</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-inlinetags">?&gt;</span></pre></td></tr></table></div></div></div><p><br class="example-break">  </p><div class="figure"><a name="fig.pdf417_ex1b"></a><p class="title"><b>Figure 25.7. Adjusting the number of columns and error correction level <code class="uri"><a class="uri" href="example_src/pdf417_ex1b.html" target="_top">(<code class="filename">pdf417_ex1b.php</code>)</a></code> </b></p><div class="figure-contents">  <span class="inlinemediaobject"><img src="images/pdf417_ex1b.png" alt="Adjusting the number of columns and error correction level (pdf417_ex1b.php)"></span> </div></div><p><br class="figure-break">
        </p>
        <p>Later on we will go through all the different options available both on the encoder
            and on the backends. But for now let's just show how easy it is to change the size of
            the barcode and add a human readable text at the bottom of the bar from the example
            above.</p>
        <p>In the same way as for linear barcode the concept of "module width" is used. This is
            basically the width of a unit bar in the barcode. Remember that each codeword is made up
            of 17 modules where there are 4 black and 4 white areas of various width. The width of
            the module is specified with the backend method</p>
        <p>
            </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
                    <p><code class="code">Backend::SetModuleWidth($aWidth)</code></p>
                </li></ul></div><p>
        </p>
        <p>For images this is specified in pixels and for a postscript backend this is
            interpretated as specifying the number of points (1 pt = 1/72 inch).</p>
        <p>To add a human readable version of the data at the bottom of the barcode we use the
            method</p>
        <p>
            </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
                    <p><code class="code">Backend::ShowText($aShow=true)</code></p>
                </li></ul></div><p>
        </p>
        <p>Adding these tow method calls to our previous example will give us the following code
            and resulting barcode.</p>
        <p>
            </p><div class="example"><a name="example.pdf417_ex2"></a><p class="title"><b>Example 25.4. Adjusting the module width and showing human readable text (<code class="filename">pdf417_ex2.php</code>) </b></p><div class="example-contents">  <div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags">&lt;?php</span><span class="hl-code"> </span><span class="hl-comment">//</span><span class="hl-comment"> content=&quot;text/plain; charset=utf-8&quot;</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">require_once</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">jpgraph/pdf417/jpgraph_pdf417.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
 
</span><span class="hl-var">$data</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">PDF-417</span><span class="hl-quotes">'</span><span class="hl-code">;
 
</span><span class="hl-comment">//</span><span class="hl-comment"> Setup some symbolic names for barcode specification</span><span class="hl-comment"></span><span class="hl-code">
 
</span><span class="hl-var">$columns</span><span class="hl-code"> = </span><span class="hl-number">8</span><span class="hl-code">;   </span><span class="hl-comment">//</span><span class="hl-comment"> Use 8 data (payload) columns</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-var">$errlevel</span><span class="hl-code"> = </span><span class="hl-number">2</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment"> Use error level 2 (minimum recommended)</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-var">$modwidth</span><span class="hl-code"> = </span><span class="hl-number">2</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment"> Setup module width (in pixels)</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-var">$height</span><span class="hl-code"> = </span><span class="hl-number">3</span><span class="hl-code">;    </span><span class="hl-comment">//</span><span class="hl-comment"> Height factor</span><span class="hl-comment"></span><span class="hl-code">
 
</span><span class="hl-comment">//</span><span class="hl-comment"> Create a new encoder and backend to generate PNG images</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">try</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-var">$encoder</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">PDF417Barcode</span><span class="hl-brackets">(</span><span class="hl-var">$columns</span><span class="hl-code">,</span><span class="hl-var">$errlevel</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-var">$backend</span><span class="hl-code"> = </span><span class="hl-identifier">PDF417BackendFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-identifier">BACKEND_IMAGE</span><span class="hl-code">,</span><span class="hl-var">$encoder</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-var">$backend</span><span class="hl-code">-&gt;</span><span class="hl-identifier">SetModuleWidth</span><span class="hl-brackets">(</span><span class="hl-var">$modwidth</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-var">$backend</span><span class="hl-code">-&gt;</span><span class="hl-identifier">SetHeight</span><span class="hl-brackets">(</span><span class="hl-var">$height</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-var">$backend</span><span class="hl-code">-&gt;</span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-var">$data</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-reserved">catch</span><span class="hl-brackets">(</span><span class="hl-identifier">JpGraphException</span><span class="hl-code"> </span><span class="hl-var">$e</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">echo</span><span class="hl-code"> </span><span class="hl-quotes">'</span><span class="hl-string">PDF417 Error: </span><span class="hl-quotes">'</span><span class="hl-code">.</span><span class="hl-var">$e</span><span class="hl-code">-&gt;</span><span class="hl-identifier">GetMessage</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-inlinetags">?&gt;</span></pre></td></tr></table></div></div></div><p><br class="example-break">  </p><div class="figure"><a name="fig.pdf417_ex2"></a><p class="title"><b>Figure 25.8. Adjusting the module width and showing human readable text <code class="uri"><a class="uri" href="example_src/pdf417_ex2.html" target="_top">(<code class="filename">pdf417_ex2.php</code>)</a></code> </b></p><div class="figure-contents">  <span class="inlinemediaobject"><img src="images/pdf417_ex2.png" alt="Adjusting the module width and showing human readable text (pdf417_ex2.php)"></span> </div></div><p><br class="figure-break">
        </p>
        <p>In the remainder of this chapter we will explain in more detail what other formatting
            options are available.</p>
    </div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"> </td><td width="20%" align="center"><a accesskey="u" href="ch25.html">Up</a></td><td width="40%" align="right"> </td></tr><tr><td width="40%" align="left" valign="top"> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> </td></tr></table></div></body></html>