tag:blogger.com,1999:blog-75460343259934618162024-03-16T11:49:49.264-07:00Asudahlah.comProgramming, tutorials, mechatronics, operating systems, and other tech stuffgemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.comBlogger44125tag:blogger.com,1999:blog-7546034325993461816.post-79466374376944077852020-10-06T21:57:00.003-07:002020-10-06T21:57:48.869-07:00Tutorial: Use Google Authentication (or other OAuth Service) in Laravel 8 project<p>In this tutorial we will use Google Authentication (or any other OAuth service like facebook, twitter, etc) as an alternative login option for your laravel application. As you can see in many apps out there, user registration is optional because user can just click "Sign in with google" or something like that.</p><h3 style="text-align: left;">How does it work</h3><p>When new user clicked the "Sign in with google" button, the application will send a request to google for user info. Google then provide the user info along with a unique ID, then the app register that info in the database because it doesn't exist yet. So now the user is a registered user.</p><p>Next time, when the user login they only need to do the same thing. The app then request the user info again, got the ID, and then check the database. Because the ID exist, the user then logged in by the system.</p><h3 style="text-align: left;">What you need</h3><p>Because we use third party service here, we need to authorize our app to request user info. Visit <a href="https://console.developers.google.com/" rel="nofollow" target="_blank">google developer console</a>. Make a project then you will got something like this.</p><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-AoX2b4Xs-Mg/X307CQcxhpI/AAAAAAAAB8o/d0gPlM8YUgArcjXGgeY0_2ny_rig-KvBwCLcBGAsYHQ/s1026/oauth_console_dasboard.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="681" data-original-width="1026" src="https://1.bp.blogspot.com/-AoX2b4Xs-Mg/X307CQcxhpI/AAAAAAAAB8o/d0gPlM8YUgArcjXGgeY0_2ny_rig-KvBwCLcBGAsYHQ/s320/oauth_console_dasboard.PNG" width="320" /></a></div><p></p><div class="separator" style="clear: both; text-align: center;"><br /></div>Then go to "OAuth Consent Screen" menu, choose "External" then Create.<div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-P5lhL_27MRM/X3071_OpS3I/AAAAAAAAB84/HS3CNY1FDgUf6HrBDOYpFgvge0u5gmAegCLcBGAsYHQ/s438/oauth_consent.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="355" data-original-width="438" src="https://1.bp.blogspot.com/-P5lhL_27MRM/X3071_OpS3I/AAAAAAAAB84/HS3CNY1FDgUf6HrBDOYpFgvge0u5gmAegCLcBGAsYHQ/s320/oauth_consent.PNG" width="320" /></a></div><br /><div>You only need to fill "Application Name" field for now. Then save.</div><div><br /></div><div>Go to "Credential" menu, then create a new OAuth Client ID.<br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/--nr9Oa8OiW0/X308lsOuP_I/AAAAAAAAB9I/8g2V2XJdyxgwfrYKa5h5YsSWsP7yWNKJwCLcBGAsYHQ/s952/oauth_create_new_id.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="552" data-original-width="952" src="https://1.bp.blogspot.com/--nr9Oa8OiW0/X308lsOuP_I/AAAAAAAAB9I/8g2V2XJdyxgwfrYKa5h5YsSWsP7yWNKJwCLcBGAsYHQ/s320/oauth_create_new_id.PNG" width="320" /></a></div>Fill out the name and the Redirect URL. In my case, i use http://localhost/gemacms/public/auth/callback/google because i hosted my app locally.</div><div>Click "save" and you will get a ClientID and Secret Key.</div><div><br /></div><h3 style="text-align: left;">Integrate OAuth to laravel project</h3><div>Add fields to your user table namely:</div><div>socialite_id,email, avatar(optional).<br /></div><div><br /></div><div>In your laravel project, install socialite library.</div><div><pre class=" language-php" style="background: rgb(251, 251, 253); box-shadow: rgba(0, 0, 0, 0.075) 0px 1px 1px; box-sizing: border-box; color: #ca473f; font-family: monospace, monospace; font-size: 16px; hyphens: none; margin-bottom: 2em; margin-top: 0.5em; max-width: 100%; overflow-wrap: normal; overflow: auto; padding: 1em; tab-size: 4; word-break: normal;"><code class=" language-php" style="background: none; box-sizing: border-box; color: #090910; font-family: source-code-pro, monospace; font-size: 0.8rem; hyphens: none; line-height: 1.9; overflow-wrap: normal; tab-size: 4; word-break: normal; word-spacing: normal;">composer <span class="token keyword" style="box-sizing: border-box; color: #055472;">require</span> laravel<span class="token operator" style="box-sizing: border-box;">/</span>socialite</code></pre><div>Then open your .env, and put :</div><div><div style="background-color: #1e1e1e; line-height: 19px;"><span style="color: #d4d4d4; font-family: Consolas, Courier New, monospace;"><span style="font-size: 14px; white-space: pre;">GOOGLE_CLIENT_ID=paste your client ID here
GOOGLE_CLIENT_SECRET=paste your secret key here
GOOGLE_CLIENT_CALLBACK=http://localhost/gemacms/public/auth/callback/google(adjust to your setting)</span></span></div></div><div><div><br /></div><div><br /></div>Then in your config/services.php, add:</div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div> <span style="color: #ce9178;">'google'</span> => [</div><div> <span style="color: #ce9178;">'client_id'</span> => <span style="color: #dcdcaa;">env</span>(<span style="color: #ce9178;">'GOOGLE_CLIENT_ID'</span>),</div><div> <span style="color: #ce9178;">'client_secret'</span> => <span style="color: #dcdcaa;">env</span>(<span style="color: #ce9178;">'GOOGLE_CLIENT_SECRET'</span>),</div><div> <span style="color: #ce9178;">'redirect'</span> => <span style="color: #dcdcaa;">env</span>(<span style="color: #ce9178;">'GOOGLE_CLIENT_CALLBACK'</span>),</div><div> ],</div></div><div class="separator" style="clear: both; text-align: center;"><br /></div>Add these 2 route in your route/web.php</div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #4ec9b0;">Route</span>::<span style="color: #dcdcaa;">get</span>(<span style="color: #ce9178;">'auth/{driver}'</span>, <span style="color: #ce9178;">'LoginController@oauth_login'</span>);</div><div><span style="color: #4ec9b0;">Route</span>::<span style="color: #dcdcaa;">get</span>(<span style="color: #ce9178;">'auth/callback/{driver}'</span>, <span style="color: #ce9178;">'LoginController@oauth_callback'</span>);</div></div></div></div><div>First route is for when the user click the "Sign in with..." button. The socialite library will then redirect to the second route when the auth is success.</div><div><br /></div><div>Now create the <b>LoginController</b> controller and create 2 method inside it:</div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div> <span style="color: #569cd6;">public</span> <span style="color: #569cd6;">function</span> <span style="color: #dcdcaa;">oauth_login</span>(<span style="color: #569cd6;">Request</span> <span style="color: #9cdcfe;">$request</span>,<span style="color: #9cdcfe;"> $driver</span>){</div><div> <span style="color: #c586c0;">return</span> <span style="color: #4ec9b0;">Socialite</span>::<span style="color: #dcdcaa;">driver</span>(<span style="color: #9cdcfe;">$driver</span>)-><span style="color: #dcdcaa;">redirect</span>();</div><div> }</div><div> <span style="color: #569cd6;">public</span> <span style="color: #569cd6;">function</span> <span style="color: #dcdcaa;">oauth_callback</span>(<span style="color: #569cd6;">Request</span> <span style="color: #9cdcfe;">$request</span>,<span style="color: #9cdcfe;"> $driver</span>){</div><div> <span style="color: #9cdcfe;">$oauthUser</span> = <span style="color: #4ec9b0;">Socialite</span>::<span style="color: #dcdcaa;">driver</span>(<span style="color: #9cdcfe;">$driver</span>)-><span style="color: #dcdcaa;">user</span>();</div><div> <span style="color: #9cdcfe;">$user</span> = <span style="color: #4ec9b0;">User</span>::<span style="color: #dcdcaa;">where</span>(<span style="color: #ce9178;">'socialite_id'</span>, <span style="color: #9cdcfe;">$oauthUser</span>->get<span style="color: #9cdcfe;">Id()</span>)-><span style="color: #dcdcaa;">first</span>();</div><div> <span style="color: #c586c0;">if</span> (<span style="color: #9cdcfe;">$user</span>) {</div><div> <span style="color: #4ec9b0;">Auth</span>::<span style="color: #dcdcaa;">loginUsingId</span>(<span style="color: #9cdcfe;">$user</span>-><span style="color: #9cdcfe;">id_users</span>);</div><div> <span style="color: #c586c0;">return</span> <span style="color: #dcdcaa;">redirect</span>(<span style="color: #ce9178;">'admin/dashboard'</span>);</div><div> } <span style="color: #c586c0;">else</span> {</div><div> <span style="color: #9cdcfe;">$newUser</span> = <span style="color: #4ec9b0;">User</span>::<span style="color: #dcdcaa;">create</span>([</div><div> <span style="color: #ce9178;">'username'</span> => <span style="color: #9cdcfe;">$oauthUser</span>-><span style="color: #dcdcaa;">getEmail</span>(),</div><div> <span style="color: #ce9178;">'roles_id'</span> => <span style="color: #b5cea8;">1</span>,</div><div> <span style="color: #ce9178;">'name'</span> => <span style="color: #9cdcfe;">$oauthUser</span>-><span style="color: #dcdcaa;">getName</span>(),</div><div> <span style="color: #ce9178;">'email'</span> => <span style="color: #9cdcfe;">$oauthUser</span>-><span style="color: #dcdcaa;">getEmail</span>(),</div><div> <span style="color: #ce9178;">'socialite_id'</span>=> <span style="color: #9cdcfe;">$oauthUser</span>-><span style="color: #dcdcaa;">getId</span>(),</div><div> <span style="color: #ce9178;">'avatar'</span>=> <span style="color: #9cdcfe;">$oauthUser</span>-><span style="color: #dcdcaa;">getAvatar</span>(),</div><div> <span style="color: #ce9178;">'password'</span> => <span style="color: #dcdcaa;">md5</span>(<span style="color: #9cdcfe;">$oauthUser</span>-><span style="color: #9cdcfe;">token</span>),</div><div> ]);</div><div> <span style="color: #4ec9b0;">Auth</span>::<span style="color: #dcdcaa;">login</span>(<span style="color: #9cdcfe;">$newUser</span>);</div><div> <span style="color: #c586c0;">return</span> <span style="color: #dcdcaa;">redirect</span>(<span style="color: #ce9178;">'admin/dashboard'</span>);</div><div> }</div><div> }</div></div></div><div><br /></div><div>When the user clicked the "Sign in with..." url, it will load the <b>oauth_login</b> method. This will load the socialite library with the specified driver (google in this case), then redirect to the url as provided in the GOOGLE_CLIENT_CALLBACK in .env (in my case, it's http://localhost/gemacms/public/auth/callback/google) . Which is basically calls the <b>oauth_callback</b> method with "google" as parameter.</div><div><br /></div><div>Then inside the <b>oauth_callback</b> we call <span style="background-color: #1e1e1e; color: #4ec9b0; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">Socialite</span><span style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">::</span><span style="background-color: #1e1e1e; color: #dcdcaa; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">driver</span><span style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">(</span><span style="background-color: #1e1e1e; color: #9cdcfe; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">$driver</span><span style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">)-></span><span style="background-color: #1e1e1e; color: #dcdcaa; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">user</span><span style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; white-space: pre;">()</span> which will provide us with these info (copied from the docs):</div><div><div><span style="font-family: courier; font-size: x-small;">// OAuth Two Providers</span></div><div><span style="font-family: courier; font-size: x-small;">$user->token;</span></div><div><span style="font-family: courier; font-size: x-small;">$user->refreshToken; // not always provided</span></div><div><span style="font-family: courier; font-size: x-small;">$user->expiresIn;</span></div><div><span style="font-family: courier; font-size: x-small;"><br /></span></div><div><span style="font-family: courier; font-size: x-small;">// OAuth One Providers</span></div><div><span style="font-family: courier; font-size: x-small;">$user->token;</span></div><div><span style="font-family: courier; font-size: x-small;">$user->tokenSecret;</span></div><div><span style="font-family: courier; font-size: x-small;"><br /></span></div><div><span style="font-family: courier; font-size: x-small;">// All Providers</span></div><div><span style="font-family: courier; font-size: x-small;">$user->getId();</span></div><div><span style="font-family: courier; font-size: x-small;">$user->getNickname();</span></div><div><span style="font-family: courier; font-size: x-small;">$user->getName();</span></div><div><span style="font-family: courier; font-size: x-small;">$user->getEmail();</span></div><div><span style="font-family: courier; font-size: x-small;">$user->getAvatar();</span></div></div><div><br /></div><div>With getId() we can get the unique user ID from google. With this ID, we can match it with the one we had in the database. If it doesn't exist yet, we create the user with info provided above. And if done or the user is already exist, we logged in the user.</div><div><br /></div><div>Keep in mind that password is not really required in this method.<br /><br />Then in your login page, add a link to auth/google :<br /><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><span style="color: grey;"><</span><span style="color: #569cd6;">a</span> <span style="color: #9cdcfe;">href</span>=<span style="color: #ce9178;">"</span><span style="color: #dcdcaa;">{{</span><span style="color: #ce9178;"> </span><span style="color: #dcdcaa;">url</span><span style="color: #ce9178;">('/auth/google') </span><span style="color: #dcdcaa;">}}</span><span style="color: #ce9178;">"</span> <span style="color: #9cdcfe;">class</span>=<span style="color: #ce9178;">"btn btn-danger btn-block"</span><span style="color: grey;">></span><span style="color: #dcdcaa;">{{</span> <span style="color: #dcdcaa;">__</span>(<span style="color: #ce9178;">'Sign in with Google'</span>) <span style="color: #dcdcaa;">}}</span><span style="color: grey;"></</span><span style="color: #569cd6;">a</span><span style="color: grey;">></span></div></div><div><br /></div><div>That's it.</div>gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com1tag:blogger.com,1999:blog-7546034325993461816.post-75436576744254193832018-11-08T23:51:00.000-08:002018-11-08T23:51:04.746-08:00PHP FPDF Use text wrapping in table with MultiCell TableWhen we make a table in pdf using PHP FPDF library, basically we build it individually cell by cell. It is of course not as convenience as making a table using xml language such as HTML table. And the major drawbacks of using Cells is it's not able to wrap or break text as it overflow the defined cell width.<br />
<h3>
MultiCell</h3>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-ijr6HjMOByg/W-U7SgCO2cI/AAAAAAAABqw/PE4WxKtwoN4PH0V5QZHpjyrv_QDA4G-6gCLcBGAs/s1600/asudahlah.com-fpdf-multicell-table-tutorial.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="641" data-original-width="1141" height="179" src="https://2.bp.blogspot.com/-ijr6HjMOByg/W-U7SgCO2cI/AAAAAAAABqw/PE4WxKtwoN4PH0V5QZHpjyrv_QDA4G-6gCLcBGAs/s320/asudahlah.com-fpdf-multicell-table-tutorial.JPG" width="320" /></a></div>
However, there is a method in FPDF library called MultiCell which allows us to make a Cell that wraps it's content, in short, it will break the text and make a new line when the text exceed it's width, thus multiplies it's height accordingly. But it doesn't works the same way with Cell, especially the positioning as the height varies according to it's content.<br />
<br />
To make a table using MultiCell, we need to find out how much lines needed in a row as every cell in a row should have same height. And then we need to define where to put next cell, we can do this by calculating current cell position and offset it with it's dimension.<br />
<br />
<h3>
Don't want the math? FPDF MultiCell Table Class to the rescue.</h3>
FPDF Founder, Olivier Plathey have provided a FPDF extension class to make table from MultiCell named PDF_MC_Table. You can copy it from here :<br />
<a href="https://github.com/gemul/fpdf-multicell-table/blob/master/pdf_mc_table.php" rel="nofollow" target="_blank">https://github.com/gemul/fpdf-multicell-table/blob/master/pdf_mc_table.php</a><br />
<br />
To use it, change the main fpdf.php file path to your FPDF library directory.<br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #0f140f; color: #ff0007; font-style: italic; font-weight: bold;"><?php</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//call main fpdf file</span>
<span style="color: #fb660a; font-weight: bold;">require</span><span style="color: white;">(</span><span style="color: #0086d2;">'<b>fpdf18/fpdf.php</b>'</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//create new class extending fpdf class</span>
<span style="color: #fb660a; font-weight: bold;">class</span> <span style="color: white;">PDF_MC_Table</span> <span style="color: #fb660a; font-weight: bold;">extends</span> <span style="color: white;">FPDF</span> <span style="color: white;">{</span>
<span style="color: white;">...</span>
</pre>
</div>
<br />
Then in your script, include the <b>pdf_mc_table.php</b> file, not <b>fpdf.php</b> file.<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #0f140f; color: #ff0007; font-style: italic; font-weight: bold;"><?php</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//include pdf_mc_table.php, not fpdf17/fpdf.php</span>
<span style="color: #fb660a; font-weight: bold;">include</span><span style="color: white;">(</span><span style="color: #0086d2;">'pdf_mc_table.php'</span><span style="color: white;">);</span>
<span style="color: white;">...</span>
</pre>
</div>
<br />
Next, create a new object from <b>PDF_MC_Table </b>Class.<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">...</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//make new object</span>
<span style="color: #fb660a;">$pdf</span> <span style="color: white;">=</span> <span style="color: #fb660a; font-weight: bold;">new</span> <span style="color: white;">PDF_MC_Table();</span>
<span style="color: white;">...</span>
</pre>
</div>
<br />
From this point onward, you can use <b>$pdf</b> to build your pdf structure as usual. It doesn't change any functionaly of FPDF but instead it does add new ones.<br />
<h3>
Build the pdf table with MultiCell.</h3>
Like usual, you can add pages, use cell to build your content, and finally call <b>Output()</b> to render your pdf. Please read <a href="https://www.asudahlah.com/2017/10/generate-printable-invoice-in-php-using.html" target="_blank">Generate Printable Invoice in PHP using FPDF library</a> for more information.<br />
And when it comes to make a multicell table, you need to:<br />
<ol>
<li><h4>
Set Font</h4>
This is important to calculate length of the text.<br />
<!-- HTML generated using hilite.me --><div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">...</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//add page, set font</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">AddPage</span><span style="color: white;">();</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">SetFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Arial'</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">14</span><span style="color: white;">);</span>
<span style="color: white;">...</span>
</pre>
</div>
</li>
<li><h4>
Define the columns width.</h4>
The column width is defined in array manner using <b>SetWidths()</b> method. The number of element should match the number of column.<!-- HTML generated using hilite.me --><div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">...</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//set width for each column (6 columns)</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">SetWidths</span><span style="color: white;">(</span><span style="color: #fb660a; font-weight: bold;">Array</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">20</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">40</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">40</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">30</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">20</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">40</span><span style="color: white;">));</span>
<span style="color: white;">...</span>
</pre>
</div>
</li>
<li><h4>
Set the line height.</h4>
Use <b>SetLineHeight()</b> to define line height. This height will be used globally in our table. What we need to define here is height of line, not the row height. For example, if we define the line height to 5, and the cell with greatest line number in a row require 3 lines, then the height of the entire row will be 5 * 3 = 15.<!-- HTML generated using hilite.me --><div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">...</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//set line height. This is the height of each lines, not rows.</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">SetLineHeight</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">);</span>
<span style="color: white;">...</span>
</pre>
</div>
</li>
<li><h4>
Set the columns alignment (optional).</h4>
This is optional. The default alginment of the cell is Left. You can define column alignment using <b>SetAligns() </b>method in array manner, with 'L' or empty string for Left (default), 'R' for Right, 'C' for Center, 'J' for justified. Also note that even if you only want to change only one column, you need to define all of them because number of array elements should match the number of columns.<br />
For example, when we want to make second column right aligned and third column centered, we use:<!-- HTML generated using hilite.me --><div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">...</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//set alignment</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">SetAligns</span><span style="color: white;">(</span><span style="color: #fb660a; font-weight: bold;">Array</span><span style="color: white;">(</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086d2;">'R'</span><span style="color: white;">,</span><span style="color: #0086d2;">'C'</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">));</span>
<span style="color: white;">...</span>
</pre>
</div>
</li>
<li><h4>
Make the table header with standard Cell.</h4>
To make it look like a proper table, add a table heading using standard cell with same number of columns and same widths for each cell respectively. Make the font bold if you like, or change the fill color to distinct it from table body. In this example, i will only make it bold.<!-- HTML generated using hilite.me --><div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">...</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//add table heading using standard cells</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//set font to bold</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">SetFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Arial'</span><span style="color: white;">,</span><span style="color: #0086d2;">'B'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">14</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">20</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">"ID"</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">40</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">"First Name"</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">40</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">"Last Name"</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">30</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">"Email"</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">20</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">"Gender"</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">40</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">"Address"</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//add a new line</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Ln</span><span style="color: white;">();</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//reset font</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">SetFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Arial'</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">14</span><span style="color: white;">);</span>
<span style="color: white;">...</span>
</pre>
</div>
Add a new line after that and do not forget to change the font back to normal.</li>
<li><h4>
Write each row with Row() method.</h4>
Then write the data row by row with <b>Row()</b> method also in array manners. The class will take care of the math.<!-- HTML generated using hilite.me --><div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">...</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//loop the data</span>
<span style="color: #fb660a; font-weight: bold;">foreach</span><span style="color: white;">(</span><span style="color: #fb660a;">$data</span> <span style="color: #fb660a; font-weight: bold;">as</span> <span style="color: #fb660a;">$item</span><span style="color: white;">){</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//write data using Row() method containing array of values.</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Row</span><span style="color: white;">(</span><span style="color: #fb660a; font-weight: bold;">Array</span><span style="color: white;">(</span>
<span style="color: #fb660a;">$item</span><span style="color: white;">[</span><span style="color: #0086d2;">'id'</span><span style="color: white;">],</span>
<span style="color: #fb660a;">$item</span><span style="color: white;">[</span><span style="color: #0086d2;">'first_name'</span><span style="color: white;">],</span>
<span style="color: #fb660a;">$item</span><span style="color: white;">[</span><span style="color: #0086d2;">'last_name'</span><span style="color: white;">],</span>
<span style="color: #fb660a;">$item</span><span style="color: white;">[</span><span style="color: #0086d2;">'email'</span><span style="color: white;">],</span>
<span style="color: #fb660a;">$item</span><span style="color: white;">[</span><span style="color: #0086d2;">'gender'</span><span style="color: white;">],</span>
<span style="color: #fb660a;">$item</span><span style="color: white;">[</span><span style="color: #0086d2;">'address'</span><span style="color: white;">],</span>
<span style="color: white;">));</span>
<span style="color: white;">}</span>
<span style="color: white;">...</span>
</pre>
</div>
</li>
</ol>
That's it. And don't forget to call <b>$pdf->Output();</b> method to see the result.<br />
<br />
And always keep in mind to <b>never output anything in your script, so no empty line before "<?php", no echo, var_dump(), print() whatsoever</b>. Even an error report or php warning will break your pdf file thus make it a corrupted pdf.<br />
<br />
Except when you want to debug your code, you can freely output anything but comment out <b>$pdf->Output();</b> line to prevent pdf render.<br />
<h3>
Known limitations.</h3>
However, as far as i know, there are some limitation in this method which is:<br />
<ol>
<li>Borders.<br />Because the border is drawn using Rect() behind the scene, you may need some tweaking if you wish to change its style, color or size. </li>
</ol>
<h3>
Further explanation and tutorial video.</h3>
For further explanation on how this class works behind the scene, please watch my tutorial video on youtube here :<br />
<a href="https://youtu.be/pELrw9P5ywM" target="_blank">https://youtu.be/pELrw9P5ywM</a><br />
Also check out my PHP PDF Video Tutorial playlist here:<br />
<a href="https://www.youtube.com/playlist?list=PLHCU7DCP4jhxnxV4Ub4YDqWiZ061t183c" target="_blank">https://www.youtube.com/playlist?list=PLHCU7DCP4jhxnxV4Ub4YDqWiZ061t183c</a>gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com70tag:blogger.com,1999:blog-7546034325993461816.post-43527017709057359812018-09-25T21:20:00.000-07:002018-09-25T21:20:27.781-07:00How to make ip address calculator with java netbeans<br />
This one is a video tutorial. And here is the <a class="zem_slink" href="http://en.wikipedia.org/wiki/Source_code" rel="wikipedia" target="_blank" title="Source code">source code</a> which is used in the tutorial.<br />
<code><span style="font-family: Times New Roman;">For step-by-step instruction, please watch the video.</span><br /><br />
<!-- HTML generated using hilite.me --></code><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><code><span style="color: #cdcaa9; font-weight: bold;">int</span> <span style="color: white;">prefix</span> <span style="color: white;">=</span> <span style="color: white;">Integer.</span><span style="color: #ff0086; font-weight: bold;">valueOf</span><span style="color: white;">(mask.</span><span style="color: #ff0086; font-weight: bold;">getText</span><span style="color: white;">());</span>
<span style="color: #cdcaa9; font-weight: bold;">int</span> <span style="color: white;">pos</span> <span style="color: white;">=</span> <span style="color: white;">prefix</span> <span style="color: white;">/</span> <span style="color: #0086f7; font-weight: bold;">8</span><span style="color: white;">;</span> <span style="background-color: #0f140f; color: #008800; font-style: italic;">//important octet position</span>
<span style="color: #cdcaa9; font-weight: bold;">int</span> <span style="color: white;">rem</span> <span style="color: white;">=</span> <span style="color: white;">prefix</span> <span style="color: white;">%</span> <span style="color: #0086f7; font-weight: bold;">8</span><span style="color: white;">;</span> <span style="background-color: #0f140f; color: #008800; font-style: italic;">//binary digit of importan octet</span>
<span style="color: white;">String</span> <span style="color: white;">binIpFull</span> <span style="color: white;">=</span> <span style="color: #0086d2;">""</span><span style="color: white;">;</span>
<span style="color: white;">String</span> <span style="color: white;">subBinary</span> <span style="color: white;">=</span> <span style="color: #0086d2;">""</span><span style="color: white;">;</span>
<span style="color: white;">String</span> <span style="color: white;">subBinaryFull</span> <span style="color: white;">=</span> <span style="color: #0086d2;">""</span><span style="color: white;">;</span>
<span style="color: white;">String</span> <span style="color: white;">binnetidFull</span> <span style="color: white;">=</span> <span style="color: #0086d2;">""</span><span style="color: white;">;</span>
<span style="color: white;">String</span> <span style="color: white;">binbcidFull</span> <span style="color: white;">=</span> <span style="color: #0086d2;">""</span><span style="color: white;">;</span>
<span style="color: white;">String</span> <span style="color: white;">netmaskFull</span> <span style="color: white;">=</span> <span style="color: #0086d2;">""</span><span style="color: white;">;</span>
<span style="color: white;">String</span> <span style="color: white;">netidFull</span> <span style="color: white;">=</span> <span style="color: #0086d2;">""</span><span style="color: white;">;</span>
<span style="color: white;">String</span> <span style="color: white;">bcidFull</span> <span style="color: white;">=</span> <span style="color: #0086d2;">""</span><span style="color: white;">;</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//loop to generate binary mask from octet pos</span>
<span style="color: #fb660a; font-weight: bold;">for</span> <span style="color: white;">(</span><span style="color: #cdcaa9; font-weight: bold;">int</span> <span style="color: white;">i</span> <span style="color: white;">=</span> <span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">;</span> <span style="color: white;">i</span> <span style="color: white;">&</span> <span style="color: white;">lt;</span> <span style="color: white;">=</span> <span style="color: #0086f7; font-weight: bold;">8</span><span style="color: white;">;</span> <span style="color: white;">i++)</span> <span style="color: white;">{</span>
<span style="color: #fb660a; font-weight: bold;">if</span> <span style="color: white;">(i</span> <span style="color: white;">&</span> <span style="color: white;">lt;</span> <span style="color: white;">=</span> <span style="color: white;">rem)</span> <span style="color: white;">{</span>
<span style="color: white;">subBinary</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"1"</span><span style="color: white;">;</span>
<span style="color: white;">}</span> <span style="color: #fb660a; font-weight: bold;">else</span> <span style="color: white;">{</span>
<span style="color: white;">subBinary</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"0"</span><span style="color: white;">;</span>
<span style="color: white;">}</span>
<span style="color: white;">}</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//netmask (integer version of subBinary</span>
<span style="color: #cdcaa9; font-weight: bold;">int</span> <span style="color: white;">netmask</span> <span style="color: white;">=</span> <span style="color: white;">Integer.</span><span style="color: #ff0086; font-weight: bold;">parseInt</span><span style="color: white;">(String.</span><span style="color: #ff0086; font-weight: bold;">valueOf</span><span style="color: white;">(subBinary),</span> <span style="color: #0086f7; font-weight: bold;">2</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//ip octets array</span>
<span style="color: #cdcaa9; font-weight: bold;">int</span><span style="color: white;">[]</span> <span style="color: white;">oct</span> <span style="color: white;">=</span> <span style="color: white;">{</span>
<span style="color: white;">Integer.</span><span style="color: #ff0086; font-weight: bold;">valueOf</span><span style="color: white;">(oct1.</span><span style="color: #ff0086; font-weight: bold;">getText</span><span style="color: white;">()),</span>
<span style="color: white;">Integer.</span><span style="color: #ff0086; font-weight: bold;">valueOf</span><span style="color: white;">(oct2.</span><span style="color: #ff0086; font-weight: bold;">getText</span><span style="color: white;">()),</span>
<span style="color: white;">Integer.</span><span style="color: #ff0086; font-weight: bold;">valueOf</span><span style="color: white;">(oct3.</span><span style="color: #ff0086; font-weight: bold;">getText</span><span style="color: white;">()),</span>
<span style="color: white;">Integer.</span><span style="color: #ff0086; font-weight: bold;">valueOf</span><span style="color: white;">(oct4.</span><span style="color: #ff0086; font-weight: bold;">getText</span><span style="color: white;">())</span>
<span style="color: white;">};</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//binary of important octet</span>
<span style="color: white;">String</span> <span style="color: white;">binOct</span> <span style="color: white;">=</span> <span style="color: white;">String.</span><span style="color: #ff0086; font-weight: bold;">format</span><span style="color: white;">(</span><span style="color: #0086d2;">"%8s"</span><span style="color: white;">,</span> <span style="color: white;">Integer.</span><span style="color: #ff0086; font-weight: bold;">toBinaryString</span><span style="color: white;">(oct[pos])).</span><span style="color: #ff0086; font-weight: bold;">replace</span><span style="color: white;">(</span><span style="color: #0086d2;">' '</span><span style="color: white;">,</span> <span style="color: #0086d2;">'0'</span><span style="color: white;">);</span>
<span style="color: white;">String</span> <span style="color: white;">binNetId</span> <span style="color: white;">=</span> <span style="color: #0086d2;">""</span><span style="color: white;">;</span>
<span style="color: white;">String</span> <span style="color: white;">binBcId</span> <span style="color: white;">=</span> <span style="color: #0086d2;">""</span><span style="color: white;">;</span>
<span style="color: #fb660a; font-weight: bold;">for</span> <span style="color: white;">(</span><span style="color: #cdcaa9; font-weight: bold;">int</span> <span style="color: white;">i</span> <span style="color: white;">=</span> <span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">;</span> <span style="color: white;">i</span> <span style="color: white;">&</span> <span style="color: white;">lt;</span> <span style="color: #0086f7; font-weight: bold;">8</span><span style="color: white;">;</span> <span style="color: white;">i++)</span> <span style="color: white;">{</span>
<span style="color: white;">String</span> <span style="color: white;">binPointer</span> <span style="color: white;">=</span> <span style="color: white;">subBinary.</span><span style="color: #ff0086; font-weight: bold;">substring</span><span style="color: white;">(i,</span> <span style="color: white;">i</span> <span style="color: white;">+</span> <span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">);</span>
<span style="color: #fb660a; font-weight: bold;">if</span> <span style="color: white;">(binPointer.</span><span style="color: #ff0086; font-weight: bold;">equals</span><span style="color: white;">(</span><span style="color: #0086d2;">"1"</span><span style="color: white;">))</span> <span style="color: white;">{</span>
<span style="color: white;">binNetId</span> <span style="color: white;">+=</span> <span style="color: white;">binOct.</span><span style="color: #ff0086; font-weight: bold;">substring</span><span style="color: white;">(i,</span> <span style="color: white;">i</span> <span style="color: white;">+</span> <span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">);</span>
<span style="color: white;">binBcId</span> <span style="color: white;">+=</span> <span style="color: white;">binOct.</span><span style="color: #ff0086; font-weight: bold;">substring</span><span style="color: white;">(i,</span> <span style="color: white;">i</span> <span style="color: white;">+</span> <span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">);</span>
<span style="color: white;">}</span> <span style="color: #fb660a; font-weight: bold;">else</span> <span style="color: white;">{</span>
<span style="color: white;">binNetId</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"0"</span><span style="color: white;">;</span>
<span style="color: white;">binBcId</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"1"</span><span style="color: white;">;</span>
<span style="color: white;">}</span>
<span style="color: white;">}</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//loop to generate display-ready strings</span>
<span style="color: #fb660a; font-weight: bold;">for</span> <span style="color: white;">(</span><span style="color: #cdcaa9; font-weight: bold;">int</span> <span style="color: white;">i</span> <span style="color: white;">=</span> <span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">;</span> <span style="color: white;">i</span> <span style="color: white;">&</span> <span style="color: white;">lt;</span> <span style="color: white;">=</span> <span style="color: #0086f7; font-weight: bold;">3</span><span style="color: white;">;</span> <span style="color: white;">i++)</span> <span style="color: white;">{</span>
<span style="color: white;">binIpFull</span> <span style="color: white;">+=</span> <span style="color: white;">String.</span><span style="color: #ff0086; font-weight: bold;">format</span><span style="color: white;">(</span><span style="color: #0086d2;">"%8s"</span><span style="color: white;">,</span> <span style="color: white;">Integer.</span><span style="color: #ff0086; font-weight: bold;">toBinaryString</span><span style="color: white;">(oct[i])).</span><span style="color: #ff0086; font-weight: bold;">replace</span><span style="color: white;">(</span><span style="color: #0086d2;">' '</span><span style="color: white;">,</span> <span style="color: #0086d2;">'0'</span><span style="color: white;">);</span>
<span style="color: #fb660a; font-weight: bold;">if</span> <span style="color: white;">(i</span> <span style="color: white;"><</span> <span style="color: white;">pos)</span> <span style="color: white;">{</span> <span style="color: white;">=</span> <span style="color: #0086d2;">""</span>
<span style="color: white;">subbinaryfull</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"11111111"</span><span style="color: white;">;</span> <span style="color: white;">=</span> <span style="color: #0086d2;">""</span>
<span style="color: white;">netmaskfull</span> <span style="color: white;">=</span> <span style="color: #0086d2;">""</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"255"</span>
<span style="color: white;">binnetidfull</span> <span style="color: white;">=</span> <span style="color: #0086d2;">""</span>
<span style="color: #0086d2;">','</span>
<span style="color: #0086f7; font-weight: bold;">0</span> <span style="color: white;">');=</span><span style="color: #0086d2;">""</span> <span style="color: white;">binbcidfull=</span><span style="color: #0086d2;">""</span> <span style="color: white;">netidfull=</span><span style="color: #0086d2;">""</span> <span style="color: white;">bcidfull=</span><span style="color: #0086d2;">""</span> <span style="color: white;">=</span><span style="color: #0086d2;">""</span> <span style="color: white;">}</span><span style="color: #fb660a; font-weight: bold;">else</span><span style="color: white;">=</span><span style="color: #0086d2;">""</span> <span style="color: #fb660a; font-weight: bold;">if</span><span style="color: white;">(i=</span><span style="color: #0086d2;">""</span><span style="color: white;">>pos){</span>
<span style="color: white;">subBinaryFull</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"00000000"</span><span style="color: white;">;</span>
<span style="color: white;">netmaskFull</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"0"</span><span style="color: white;">;</span>
<span style="color: white;">binnetidFull</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"00000000"</span><span style="color: white;">;</span>
<span style="color: white;">binbcidFull</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"00000000"</span><span style="color: white;">;</span>
<span style="color: white;">netidFull</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"0"</span><span style="color: white;">;</span>
<span style="color: white;">bcidFull</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"1"</span><span style="color: white;">;</span>
<span style="color: white;">}</span> <span style="color: #fb660a; font-weight: bold;">else</span> <span style="color: white;">{</span>
<span style="color: white;">subBinaryFull</span> <span style="color: white;">+=</span> <span style="color: white;">subBinary;</span>
<span style="color: white;">netmaskFull</span> <span style="color: white;">+=</span> <span style="color: white;">String.</span><span style="color: #ff0086; font-weight: bold;">valueOf</span><span style="color: white;">(netmask);</span>
<span style="color: white;">binnetidFull</span> <span style="color: white;">+=</span> <span style="color: white;">binNetId;</span>
<span style="color: white;">binbcidFull</span> <span style="color: white;">+=</span> <span style="color: white;">binBcId;</span>
<span style="color: white;">netidFull</span> <span style="color: white;">+=</span> <span style="color: white;">Integer.</span><span style="color: #ff0086; font-weight: bold;">parseInt</span><span style="color: white;">(String.</span><span style="color: #ff0086; font-weight: bold;">valueOf</span><span style="color: white;">(binNetId),</span> <span style="color: #0086f7; font-weight: bold;">2</span><span style="color: white;">);</span>
<span style="color: white;">bcidFull</span> <span style="color: white;">+=</span> <span style="color: white;">Integer.</span><span style="color: #ff0086; font-weight: bold;">parseInt</span><span style="color: white;">(String.</span><span style="color: #ff0086; font-weight: bold;">valueOf</span><span style="color: white;">(binBcId),</span> <span style="color: #0086f7; font-weight: bold;">2</span><span style="color: white;">);</span>
<span style="color: white;">}</span>
<span style="color: #fb660a; font-weight: bold;">if</span> <span style="color: white;">(i</span> <span style="color: white;">!=</span> <span style="color: #0086f7; font-weight: bold;">3</span><span style="color: white;">)</span> <span style="color: white;">{</span>
<span style="color: white;">subBinaryFull</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"."</span><span style="color: white;">;</span>
<span style="color: white;">netmaskFull</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"."</span><span style="color: white;">;</span>
<span style="color: white;">binnetidFull</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"."</span><span style="color: white;">;</span>
<span style="color: white;">binbcidFull</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"."</span><span style="color: white;">;</span>
<span style="color: white;">netidFull</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"."</span><span style="color: white;">;</span>
<span style="color: white;">bcidFull</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"."</span><span style="color: white;">;</span>
<span style="color: white;">binIpFull</span> <span style="color: white;">+=</span> <span style="color: #0086d2;">"."</span><span style="color: white;">;</span>
<span style="color: white;">}</span>
<span style="color: white;">}</span>
<span style="color: white;">subBinLabel.</span><span style="color: #ff0086; font-weight: bold;">setText</span><span style="color: white;">(subBinaryFull);</span>
<span style="color: white;">netMaskLabel.</span><span style="color: #ff0086; font-weight: bold;">setText</span><span style="color: white;">(netmaskFull);</span>
<span style="color: white;">netBinLabel.</span><span style="color: #ff0086; font-weight: bold;">setText</span><span style="color: white;">(binnetidFull);</span>
<span style="color: white;">bcBinLabel.</span><span style="color: #ff0086; font-weight: bold;">setText</span><span style="color: white;">(binbcidFull);</span>
<span style="color: white;">netIdLabel.</span><span style="color: #ff0086; font-weight: bold;">setText</span><span style="color: white;">(netidFull);</span>
<span style="color: white;">bcIdLabel.</span><span style="color: #ff0086; font-weight: bold;">setText</span><span style="color: white;">(bcidFull);</span>
<span style="color: white;">ipBinLabel.</span><span style="color: #ff0086; font-weight: bold;">setText</span><span style="color: white;">(binIpFull);</span>
</code></pre>
</div>
<code>
</code><br />
<br />
You can watch the video here:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://www.youtube.com/watch?v=iGX_6d5Kmas" target="_blank"><iframe allowfullscreen="" class="YOUTUBE-iframe-video" data-thumbnail-src="https://i.ytimg.com/vi/iGX_6d5Kmas/0.jpg" frameborder="0" height="266" src="https://www.youtube.com/embed/iGX_6d5Kmas?feature=player_embedded" width="320"></iframe></a></div>
<br />
<br />gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com6tag:blogger.com,1999:blog-7546034325993461816.post-12247548758085765952018-09-02T04:53:00.002-07:002018-09-02T04:59:26.693-07:00[PHP FPDF] How to Integrate QR Code in PDF Generation After long time without update, this time, i will post about how to use QR Code in PDF created with PHP FPDF Library.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-jTrc4-FUbZk/W4vLc3DgU9I/AAAAAAAABp4/U4DudWHNv_ottGxXFdZx2CqdYLActd6kgCEwYBhgL/s1600/asudahlah.com-qrcode006.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="254" data-original-width="375" height="216" src="https://1.bp.blogspot.com/-jTrc4-FUbZk/W4vLc3DgU9I/AAAAAAAABp4/U4DudWHNv_ottGxXFdZx2CqdYLActd6kgCEwYBhgL/s320/asudahlah.com-qrcode006.JPG" width="320" /></a></div>
<br />
For those who doesn't know what QR Code is, here's a brief summary. QR Code, invented in 1994 by the Japanese company Denso Wave, is a 2 dimensional version of barcode. And unlike barcode, QR Code are capable to store alphanumeric, symbol, binaries, and kanji. Right now, it's widely used in many application such as product identification, payment, data entries, advertisement, announcement, and many more. In short, it looks like this:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-pCP92JG310U/W4u-fX8Wy1I/AAAAAAAABo8/kpi4jUT4wiIUR7rEs7H2KzKWtp2sKAv7QCLcBGAs/s1600/asudahlah.com-qrcode001.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="166" data-original-width="166" src="https://1.bp.blogspot.com/-pCP92JG310U/W4u-fX8Wy1I/AAAAAAAABo8/kpi4jUT4wiIUR7rEs7H2KzKWtp2sKAv7QCLcBGAs/s1600/asudahlah.com-qrcode001.JPG" /></a></div>
And you can scan it with your smartphone to get information contained in it.<br />
<br />
Now, let's get back to the topic.<br />
<br />
In my previous tutorial about <a class="K3JSBVB-c-g" href="http://www.asudahlah.com/2018/04/how-to-add-barcode-to-pdf-created-with.html" target="_blank">How to add barcode to PDF created with PHP FPDF</a>, we extend our FPDF class so it could provide barcode generation suport. But this time, we are using an entirely independent library called <a href="http://phpqrcode.sourceforge.net/" target="_blank">PHP QR Code</a> by deltalab.<br />
Download the PHP QR Code library here : <a href="http://sourceforge.net/projects/phpqrcode/files/">http://sourceforge.net/projects/phpqrcode/files/</a><br />
<br />
Then extract it to your working directory alongside the fpdf directory.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-NWchD1cbr2Y/W4vAxdVfnVI/AAAAAAAABpI/JyAka8z5-UgMon-ozMvAet53TZMsOA4TACLcBGAs/s1600/asudahlah.com-qrcode002.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="242" data-original-width="574" height="134" src="https://4.bp.blogspot.com/-NWchD1cbr2Y/W4vAxdVfnVI/AAAAAAAABpI/JyAka8z5-UgMon-ozMvAet53TZMsOA4TACLcBGAs/s320/asudahlah.com-qrcode002.JPG" width="320" /></a></div>
<br />
Then from the browser, try to open that directory to try the demos. If nothing goes wrong, you will be presented by something like this.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-6KMc90CCIIU/W4vBG4l5CEI/AAAAAAAABpQ/3vBhdbjz9j49nbdZqDGon-ZM8FOQFKALgCLcBGAs/s1600/asudahlah.com-qrcode003.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="293" data-original-width="501" height="187" src="https://1.bp.blogspot.com/-6KMc90CCIIU/W4vBG4l5CEI/AAAAAAAABpQ/3vBhdbjz9j49nbdZqDGon-ZM8FOQFKALgCLcBGAs/s320/asudahlah.com-qrcode003.JPG" width="320" /></a></div>
<br />
<br />
<blockquote class="tr_bq">
This is a general-purpose QR Code generator, which means you can use it in another project other than PDF.</blockquote>
<br />
Now, create a new php file in your working directory. Then make a blank pdf page out of it.<br />
Please read <a href="http://www.asudahlah.com/2017/10/generate-printable-invoice-in-php-using.html" target="_blank">Generate Printable Invoice in PHP using FPDF library</a> if you haven't done so.<br />
<br />
Add this code in your new file:<br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #0f140f; color: #ff0007; font-style: italic; font-weight: bold;"><?php</span>
<span style="color: #fb660a; font-weight: bold;">require_once</span><span style="color: white;">(</span><span style="color: #0086d2;">"fpdf17/fpdf.php"</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span> <span style="color: white;">=</span> <span style="color: #fb660a; font-weight: bold;">new</span> <span style="color: white;">FPDF(</span><span style="color: #0086d2;">'P'</span><span style="color: white;">,</span><span style="color: #0086d2;">'mm'</span><span style="color: white;">,</span><span style="color: #0086d2;">'A4'</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">AddPage</span><span style="color: white;">();</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Output</span><span style="color: white;">();</span>
</pre>
</div>
And the result should be a blank pdf page like this<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-IsD331hh7-c/W4vDeEeiDeI/AAAAAAAABpc/acrs5v8DX_EAy_DtGf7FoON-onU48vfWACLcBGAs/s1600/asudahlah.com-qrcode004.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="374" data-original-width="893" height="134" src="https://1.bp.blogspot.com/-IsD331hh7-c/W4vDeEeiDeI/AAAAAAAABpc/acrs5v8DX_EAy_DtGf7FoON-onU48vfWACLcBGAs/s320/asudahlah.com-qrcode004.JPG" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<span class="" style="margin-left: 1em; margin-right: 1em;"></span></div>
<br />
<br />
Before we continue, there are 2 method to insert the QR Code into our PDF File. And both works by using FPDF Image method to insert an image in the pdf.<br />
<br />
First method is by using a QR Code generator, second is generating a QR Code image file upon PDF Generation.<br />
<br />
Let's try out the former.<br />
<h3>
Create a QR Code Generator. </h3>
Create a new php file, name it <b>qr_generator.php</b>. Then add these 2 simple lines of code in it.<br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #0f140f; color: #ff0007; font-style: italic; font-weight: bold;"><?php</span>
<span style="color: #fb660a; font-weight: bold;">require_once</span><span style="color: white;">(</span><span style="color: #0086d2;">"phpqrcode/qrlib.php"</span><span style="color: white;">);</span>
<span style="color: white;">QRcode::</span><span style="color: #ff0086; font-weight: bold;">png</span><span style="color: white;">(</span><span style="color: #fb660a;">$_GET</span><span style="color: white;">[</span><span style="color: #0086d2;">'code'</span><span style="color: white;">]);</span>
</pre>
</div>
Then try to access that file in browser and provide value to code parameter like this:<br />
http://localhost/pdf/qr_generator.php?code=1234567<br />
and you should get something like this<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-yQ7f5JSoxGU/W4vE_LSGK3I/AAAAAAAABpo/MWRn_cJ8EjU3k5C8HnieFvvumaBKADoQACLcBGAs/s1600/asudahlah.com-qrcode005.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="154" data-original-width="211" src="https://1.bp.blogspot.com/-yQ7f5JSoxGU/W4vE_LSGK3I/AAAAAAAABpo/MWRn_cJ8EjU3k5C8HnieFvvumaBKADoQACLcBGAs/s1600/asudahlah.com-qrcode005.JPG" /></a></div>
Congratulation, you've just made a general-purpose QR code generator. Now let's embed it into our PDF Generator.<br />
Simply just add an image using FPDF Image method with that url as the image source parameter like this<br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #0f140f; color: #ff0007; font-style: italic; font-weight: bold;"><?php</span>
<span style="color: #fb660a; font-weight: bold;">require_once</span><span style="color: white;">(</span><span style="color: #0086d2;">"fpdf17/fpdf.php"</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span> <span style="color: white;">=</span> <span style="color: #fb660a; font-weight: bold;">new</span> <span style="color: white;">FPDF(</span><span style="color: #0086d2;">'P'</span><span style="color: white;">,</span><span style="color: #0086d2;">'mm'</span><span style="color: white;">,</span><span style="color: #0086d2;">'A4'</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">AddPage</span><span style="color: white;">();</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Image</span><span style="color: white;">(</span><span style="color: #0086d2;">"http://localhost/pdf/qr_generator.php?code=content here"</span><span style="color: white;">,</span> <span style="color: #0086f7; font-weight: bold;">10</span><span style="color: white;">,</span> <span style="color: #0086f7; font-weight: bold;">10</span><span style="color: white;">,</span> <span style="color: #0086f7; font-weight: bold;">20</span><span style="color: white;">,</span> <span style="color: #0086f7; font-weight: bold;">20</span><span style="color: white;">,</span> <span style="color: #0086d2;">"png"</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Output</span><span style="color: white;">();</span>
</pre>
</div>
and you should get something like this<br />
<br />
Just change the code parameter and image position to suit what you need. With this, you can use the qr code generator for your other project. Just make sure to secure your generator with secret key or tokens, i won't explain it here but to help you start, just use a simple IF statement to check if there are specified secret key passed in the GET request and immediately exit the code if it turns out false.<br />
<br />
Now let's try the second method.<br />
<h3>
Create the QR Code upon PDF Generation.</h3>
With this method, instead of using separate QR code generator. We are generating the QR image inside our PDF generator script, store the QR image in a directory, then insert the image using FPDF Image method.<br />
<br />
First, include the phpqrcode library in your file.<br />
Then create the QR code using QRcode::png method<br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">QRcode::</span><span style="color: #ff0086; font-weight: bold;">png</span><span style="color: white;">(</span><span style="color: #0086d2;">"coded number here"</span><span style="color: white;">,</span><span style="color: #0086d2;">"test.png"</span><span style="color: white;">);</span>
</pre>
</div>
Note that i added second parameter to the method which indicates that the resulted file will be saved as an png file with that name. <br />
<br />
Then add that image in the pdf using Image method like usual. So your final code should look like this (I've provided comments to help explain each parts of the code) :<br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #0f140f; color: #ff0007; font-style: italic; font-weight: bold;"><?php</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//include the libraries</span>
<span style="color: #fb660a; font-weight: bold;">require_once</span><span style="color: white;">(</span><span style="color: #0086d2;">"fpdf17/fpdf.php"</span><span style="color: white;">);</span>
<span style="color: #fb660a; font-weight: bold;">require_once</span><span style="color: white;">(</span><span style="color: #0086d2;">"phpqrcode/qrlib.php"</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//create a QR Code and save it as a png image file named test.png</span>
<span style="color: white;">QRcode::</span><span style="color: #ff0086; font-weight: bold;">png</span><span style="color: white;">(</span><span style="color: #0086d2;">"coded number here"</span><span style="color: white;">,</span><span style="color: #0086d2;">"test.png"</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//pdf stuff</span>
<span style="color: #fb660a;">$pdf</span> <span style="color: white;">=</span> <span style="color: #fb660a; font-weight: bold;">new</span> <span style="color: white;">FPDF(</span><span style="color: #0086d2;">'P'</span><span style="color: white;">,</span><span style="color: #0086d2;">'mm'</span><span style="color: white;">,</span><span style="color: #0086d2;">'A4'</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">AddPage</span><span style="color: white;">();</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//this is the first method</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Image</span><span style="color: white;">(</span><span style="color: #0086d2;">"http://localhost/pdf/qr_generator.php?code=content here"</span><span style="color: white;">,</span> <span style="color: #0086f7; font-weight: bold;">10</span><span style="color: white;">,</span> <span style="color: #0086f7; font-weight: bold;">10</span><span style="color: white;">,</span> <span style="color: #0086f7; font-weight: bold;">20</span><span style="color: white;">,</span> <span style="color: #0086f7; font-weight: bold;">20</span><span style="color: white;">,</span> <span style="color: #0086d2;">"png"</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//this is the second method</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Image</span><span style="color: white;">(</span><span style="color: #0086d2;">"test.png"</span><span style="color: white;">,</span> <span style="color: #0086f7; font-weight: bold;">40</span><span style="color: white;">,</span> <span style="color: #0086f7; font-weight: bold;">10</span><span style="color: white;">,</span> <span style="color: #0086f7; font-weight: bold;">20</span><span style="color: white;">,</span> <span style="color: #0086f7; font-weight: bold;">20</span><span style="color: white;">,</span> <span style="color: #0086d2;">"png"</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//and absolutely don't forget this one</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Output</span><span style="color: white;">();</span>
</pre>
</div>
<br />
And if nothing goes wrong, you should get something like this:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-jTrc4-FUbZk/W4vLc3DgU9I/AAAAAAAABp0/i9iZLP23JFElxUHQ9Zn9l-p8DWcf5HZOQCLcBGAs/s1600/asudahlah.com-qrcode006.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="254" data-original-width="375" height="216" src="https://1.bp.blogspot.com/-jTrc4-FUbZk/W4vLc3DgU9I/AAAAAAAABp0/i9iZLP23JFElxUHQ9Zn9l-p8DWcf5HZOQCLcBGAs/s320/asudahlah.com-qrcode006.JPG" width="320" /></a></div>
<b>IMPORTANT NOTICE : </b>In real world practice, it's necessary to use different file name for each PHP session because the png file would be replaced at every request. And also don't forget to remove the file regularly to prevent it from bloating your storage with a lot of unused png file.<br />
<br />
Both method have its pros and cons, just pick whichever method suit your need.<br />
<br />
Finally, please check out my youtube video here and please kindly subscribe to my channel for more tutorials. <a href="https://www.youtube.com/watch?v=XFpuxgpUXgE">https://www.youtube.com/watch?v=XFpuxgpUXgE</a><br />
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen="" class="YOUTUBE-iframe-video" data-thumbnail-src="https://i.ytimg.com/vi/XFpuxgpUXgE/0.jpg" frameborder="0" height="266" src="https://www.youtube.com/embed/XFpuxgpUXgE?feature=player_embedded" width="320"></iframe></div>
<br />
<br />
<br />gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com22tag:blogger.com,1999:blog-7546034325993461816.post-17254737457548326522018-04-15T01:30:00.001-07:002018-05-28T23:39:45.516-07:00How to add barcode to PDF created with PHP FPDFThis time, i'm about to explain how to add barcode to your generated pdf file. This tutorial requires you to understand the basics how to make / generate pdf file in PHP using FPDF library beforehand. So please read <a href="http://www.asudahlah.com/2017/10/generate-printable-invoice-in-php-using.html" target="_blank">Generate Printable Invoice in PHP using FPDF library</a> if you haven't done so.<br />
<h3>
Why and what is barcode?</h3>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-mQStb60K1Kc/WtMNhd5nk0I/AAAAAAAABMk/l43noUVHNYAuG8Q1jZTALZF7BJuwVoxCgCLcBGAs/s1600/pdf-barcode-2_www.asudahlah.com.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="279" data-original-width="613" height="145" src="https://2.bp.blogspot.com/-mQStb60K1Kc/WtMNhd5nk0I/AAAAAAAABMk/l43noUVHNYAuG8Q1jZTALZF7BJuwVoxCgCLcBGAs/s320/pdf-barcode-2_www.asudahlah.com.JPG" width="320" /></a></div>
Barcode is an optical, machine-readable numeric data consisting of series of black and white bars with varying thickness. Commonly used in label which requires fast-reading done by machine or computer. Barcode can minimize user's manual input so instead of showing only numeric id on your, let's say, invoice. You can also provide barcoded id. So the user, especially in fast-paced transaction environment can just scan it using barcode reader instead of manual input with keyboard. And here's how to add barcode to your generated PDF file in PHP FPDF library.<br />
<h3>
Barcode support for PHP FPDF library.</h3>
There are no built-in barcode support in PHP FPDF library. However, with a custom class, we can add a barcode support. Here i have prepared a barcode class in my github repository you can download right away here: <a href="https://github.com/gemul/fpdf-barcode" rel="nofollow" target="_blank">https://github.com/gemul/fpdf-barcode</a>.<br />
There are a lot of barcode standard format, but here we will only use 2 format. EAN13, and UPC_A format which is a commonly used barcode standard.<br />
EAN stands for European Article Number which is a barcode symbology standard and numbering system used in global trade to identify a specific retail product type, in a specific packaging configuration, from a specific manufacturer. While the UPC stands for Universal Product Code which is also a barcode symbology that is widely used in the United States, Canada, United Kingdom, Australia, New Zealand, in Europe and other countries for tracking trade items in stores. Both can hold 12 digit of integer (last digit in EAN13 is a check number so you can only keep 12 digit). For more explanation about barcode standard, you can look it up in wikipedia.<br />
<h3>
Using custom PHP FPDF class to make a barcode.</h3>
First, you need the PHP FPDF library, here i used FPDF version 1.7 which you can download <a href="http://www.fpdf.org/en/dl.php?v=17&f=zip" rel="nofollow" target="_blank">here</a>.<br />
Then download or copy-paste the fphp_barcode.php file from my repository to your local webserver.<br />
Open the file and make sure that the second line refer to your fpdf.php file which comes with your fpdf library. On my case, the folder structure looks like this:<br />
<span style="font-family: "courier new" , "courier" , monospace; font-size: x-small;">\htdocs\pdf\</span><br />
<span style="font-family: "courier new" , "courier" , monospace; font-size: x-small;"> |-fpdf\</span><br />
<span style="font-family: "courier new" , "courier" , monospace; font-size: x-small;"> | |-fpdf.php</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><span style="font-size: x-small;"> |-fpdf_barcode.php</span> </span><br />
So the "require" path should look like this:<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #fb660a; font-weight: bold;">require</span><span style="color: white;">(</span><span style="color: #0086d2;">'fpdf/fpdf.php'</span><span style="color: white;">);</span>
</pre>
</div>
<br />
Next, make a new file in the same directory as your fpdf_barcode.php file named barcode-test.php then copy this script into it.<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #0f140f; color: #ff0007; font-style: italic; font-weight: bold;"><?php</span>
<span style="color: #fb660a; font-weight: bold;">require</span><span style="color: white;">(</span><span style="color: #0086d2;">'fpdf_barcode.php'</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span> <span style="color: white;">=</span> <span style="color: #fb660a; font-weight: bold;">new</span> <span style="color: white;">PDF_BARCODE(</span><span style="color: #0086d2;">'P'</span><span style="color: white;">,</span><span style="color: #0086d2;">'mm'</span><span style="color: white;">,</span><span style="color: #0086d2;">'A4'</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">AddPage</span><span style="color: white;">();</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//EAN13 test</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">EAN13</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">10</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">10</span><span style="color: white;">,</span><span style="color: #0086d2;">'123456789012'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0.5</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">9</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">EAN13</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">10</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">20</span><span style="color: white;">,</span><span style="color: #0086d2;">'123456789012'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0.35</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">9</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">EAN13</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">10</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">30</span><span style="color: white;">,</span><span style="color: #0086d2;">'123456789012'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">10</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0.35</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">9</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//UPC_A test</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">UPC_A</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">100</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">10</span><span style="color: white;">,</span><span style="color: #0086d2;">'123456789012'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0.5</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">9</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">UPC_A</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">100</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">20</span><span style="color: white;">,</span><span style="color: #0086d2;">'123456789012'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0.35</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">9</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">UPC_A</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">100</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">30</span><span style="color: white;">,</span><span style="color: #0086d2;">'123456789012'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">10</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0.35</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">9</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Output</span><span style="color: white;">();</span>
</pre>
</div>
<br />
Save it then see the result in your browser. If nothing goes wrong, it should be like this:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-qreh49mtm2E/WtMEVHhrjII/AAAAAAAABMU/53MQM1HPdek1BB0zIwhRav1UzftB2zs2gCLcBGAs/s1600/pdf-barcode_www.asudahlah.com.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="530" data-original-width="899" height="188" src="https://4.bp.blogspot.com/-qreh49mtm2E/WtMEVHhrjII/AAAAAAAABMU/53MQM1HPdek1BB0zIwhRav1UzftB2zs2gCLcBGAs/s320/pdf-barcode_www.asudahlah.com.JPG" width="320" /></a></div>
<br />
Now, what does the code above do?<br />
First line, we include the fpdf_barcode.php file, not the usual fpdf.php file. We're using a custom class here.<br />
Next, make a PDF_BARCODE object, not FPDF object. With the same parameter as usual. Then add a page. And at the last line, don't forget to output the result. (Please read <a href="http://www.asudahlah.com/2017/10/generate-printable-invoice-in-php-using.html" target="_blank">this fpdf basic</a> if you haven't done so).<br />
With these lines, you are properly made a single page blank PDF file. <br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #0f140f; color: #ff0007; font-style: italic; font-weight: bold;"><?php</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//call the fpdf_barcode.php, not the usual fpdf.php. We're using a custom class here.</span>
<span style="color: #fb660a; font-weight: bold;">require</span><span style="color: white;">(</span><span style="color: #0086d2;">'fpdf_barcode.php'</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//define a PDF_BARCODE object, not FPDF object. With portrait orientation, milimeters unit, and A4 size.</span>
<span style="color: #fb660a;">$pdf</span> <span style="color: white;">=</span> <span style="color: #fb660a; font-weight: bold;">new</span> <span style="color: white;">PDF_BARCODE(</span><span style="color: #0086d2;">'P'</span><span style="color: white;">,</span><span style="color: #0086d2;">'mm'</span><span style="color: white;">,</span><span style="color: #0086d2;">'A4'</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//add a page</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">AddPage</span><span style="color: white;">();</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//the content goes here</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//Output the result</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Output</span><span style="color: white;">();</span>
</pre>
</div>
<br />
In the pdf_barcode class, there are 2 methods called EAN13 and UPC_A. As the name implies, the former is to make a EAN13 barcode and the later is for UPC_A.<br />
<br />
For EAN13, use :<br />
$pdf->EAN13(float x, float y, string barcode [, float h [, float w[, int font_size]]])<br />
<br />
And For UPC_A :<br />
$pdf->UPC_A(float x, float y, string barcode [, float h [, float w[, int font_size]]])<br />
<br />
<b>x</b> is the abscissa of barcode. Which means the horizontal position of the top-left corner of the barcode.<br />
<b>y</b> is the ordinate of barcode. The vertical position of the top-left corner of the barcode.<br />
<b>barcode</b> is the value of barcode, consists of 12 digits integer.<br />
<b>h</b> is the height of barcode. Default value is 16.<br />
<b>w</b> is the width of a bar. Default value is 0.35.<br />
<b>font_size</b> is the font size of the number below barcode.<br />
<br />
So, this line :<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">EAN13</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">10</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">10</span><span style="color: white;">,</span><span style="color: #0086d2;">'123456789012'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0.5</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">9</span><span style="color: white;">);</span>
</pre>
</div>
Means that we make an EAN13 Barcode at position of 10mm from left and 10mm from top of the page. The barcode contains 123456789012 and the height is 5 and the bar width (not the whole barcode width) is 0.5. And the text which says the integer value below the barcode have a size of 9 points.<br />
<br />
If by any chance you're still confused after following this tutorial, please do enjoy this <a href="https://www.youtube.com/watch?v=nZCvHXt2AyI" target="_blank">video-version</a> of this tutorial. And if you want the source code, feel free download it from the link provided in the description or ask me in the comment section of aforementioned video.gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com52tag:blogger.com,1999:blog-7546034325993461816.post-63219479373227618712017-11-17T06:20:00.000-08:002017-11-17T06:20:20.153-08:00Fixing mysql crash caused by InnoDB<h3>
The Problem</h3>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-heUlfdSFvTk/Wg7tUlI-5RI/AAAAAAAABKA/7yUcuqsSkb8dhPAJrnTxKPoYaIu1Mla4QCLcBGAs/s1600/mysql.PNG" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" data-original-height="271" data-original-width="548" height="158" src="https://2.bp.blogspot.com/-heUlfdSFvTk/Wg7tUlI-5RI/AAAAAAAABKA/7yUcuqsSkb8dhPAJrnTxKPoYaIu1Mla4QCLcBGAs/s320/mysql.PNG" width="320" /></a></div>
I ran a moodle application on ubuntu inside virtual machine (virtualbox). It has been working fine for a long time until one day, it decided to stop working in the middle of an exam participated by -+ 40 students. It shows that the database is somehow crashed. <br />
<br />
<br />
<h3>
Diagnosis</h3>
I tried to open the mysql using mysql client, and i'm unable to.<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">ERROR 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock'</span>
</pre>
</div>
I check the state of mysql server and turn out it have forcibly stopped, so i try to run it again using:<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">/etc/init.d/mysql start</span>
</pre>
</div>
and get<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">start: Job failed to start</span>
</pre>
</div>
<br />
First, because i ran the system in a vm, i thought it was either the disk space or memory, so i ran <b>df</b> command then found out it currently use only 5% of the total disk spaces. And then i ran <b>htop</b> and it only used <500MB from the total of 8GB allocated to the vm so i ruled out both possibility.<br />
<br />
Ok, so i know there are something definitely wrong with the mysql server. So, just in case, <b>i made a backup</b> to the mysql data directory into my home dir (and you should do to, backup is very important afterall)<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">sudo cp -rp /var/lib/mysql ~/mysql/</span>
</pre>
</div>
The <b>-p</b> option here is to keep the permission, ownership and timestamp intact.<br />
<br />
I checked the mysql logs:<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">cat /var/log/mysql.log</span>
<span style="color: white;">cat /var/log/mysql.err</span>
</pre>
</div>
both shows nothing, so i checked the error log<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">cat /var/log/mysql.err</span>
</pre>
</div>
And checking some latest lines, i got<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">mysqld got signal 11 ;</span>
<span style="color: white;">This could be because you hit a bug. It is also possible that this binary</span>
<span style="color: white;">or one of the libraries it was linked against is corrupt, improperly built,</span>
<span style="color: white;">or misconfigured. This error can also be caused by malfunctioning hardware.</span>
<span style="color: white;">Attempting to collect some information that could help diagnose the problem.</span>
<span style="color: white;">As this is a crash and something is definitely wrong, the information</span>
<span style="color: white;">collection process might fail.</span>
<span style="color: white;">......</span>
</pre>
</div>
And bunch of log text which i didn't particularly understood, stating that mysql server is crashing.<br />
<br />
<h3>
Solution</h3>
After scouring the web for solution, i came across a possibility that my innodb database is corrupted. So as a trial and error, i remove these 3 files:<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">/var/lib/mysql/ibdata1</span>
<span style="color: white;">/var/lib/mysql/ib_logfile0</span>
<span style="color: white;">/var/lib/mysql/ib_logfile1</span>
</pre>
</div>
Then try to run the mysql server again using <b>/etc/init.d/mysql start</b> and it runs. With this, i am sure that it's the innodb fault.<br />
Before continuing, stopped the mysql server again, then copy those 3 files from the backup i've made, then start the mysql server again. As expected, it failed to start.<br />
<br />
After scouring the web again, i've found a solution to run mysql server in innodb force recovery mode. I open /etc/mysql/my.cnf using nano, then add these lines:<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">innodb_force_recovery = 2</span>
<span style="color: white;">innodb_purge_threads = 0</span>
</pre>
</div>
As described <a href="https://dev.mysql.com/doc/refman/5.7/en/forcing-innodb-recovery.html" target="_blank">here</a>, i tried with number 1 first, then continue upward until the server could run. Found out that my magic number is 2. Which means preventing master and purge threads from running.<br />
<blockquote class="tr_bq">
Taken from the link above:<br />
2 (SRV_FORCE_NO_BACKGROUND)<br />
Prevents the master thread and any purge threads from running. If a crash would occur during the purge operation, this recovery value prevents it. </blockquote>
After the mysql server runs, i connect to the mysql using<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">mysql -u root -p</span>
</pre>
</div>
to make sure that my databases and tables are still there. And they are.<br />
In this state, you won't be able to insert or update anything in the database. You can only read and drop things. So the idea here is to dump the corrupted table to a file somewhere, drop the table from database, and then import the dumped file back into database.<br />
<br />
I run mysqlcheck to see which tables is corrupted:<br />
mysqlcheck -u root -p --all-databases<br />
and it says all my tables is OK.<br />
<blockquote class="tr_bq">
in my case, i have to pipe the command to "more" command because i have a lot of tables.<br />
<i>mysqlcheck -u root -p --all-databases | more</i></blockquote>
This confuse me as i don't know which table i should dump. So i dump my database into a file as a backup. Luckily, my database is not too big to dump.<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">mysqldump -u root -p [pass] [db name] > [db name].sql</span>
</pre>
</div>
and it produce a single -+ 600MB sql file for a database in my case (it still pretty big for me).<br />
<br />
If in your case you found out the corrupted table when you do mysqlcheck, you should only dump that corrupted table to avoid bloating the dump file. Especially when your database is big.<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: white;">mysqldump -u root -p [pass] [db name] [table name] > [db name].[table name].sql</span>
</pre>
</div>
<br />
Then i drop the whole database, create a new database with same name, and import the dumped file back into that database.<br />
<br />
Be careful, in your case, you might only need to drop the corrupted table, not the whole database.<br />
<br />
And after the import proccess is finished. My moodle application can run normally.<br />
<br />gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com1tag:blogger.com,1999:blog-7546034325993461816.post-63175008357700770972017-11-05T01:40:00.000-07:002018-06-03T01:12:30.513-07:00Create, Add, and Use Custom Font in PHP FPDF Library<a href="https://3.bp.blogspot.com/-bANI-d2ELfk/Wf7UA8oQ1LI/AAAAAAAABJY/7dl7Ehqnl94M1yBZskfGi7pHLw6D9t_fQCLcBGAs/s1600/fpdf2-5.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" data-original-height="616" data-original-width="1108" height="177" src="https://3.bp.blogspot.com/-bANI-d2ELfk/Wf7UA8oQ1LI/AAAAAAAABJY/7dl7Ehqnl94M1yBZskfGi7pHLw6D9t_fQCLcBGAs/s320/fpdf2-5.jpg" width="320" /></a>By default, fonts which can be used to generate pdf file using PHP FPDF Library are only predefined standard fonts bundled along FPDF Library such as Courier, Helvetica, Arial, Times, Symbol, and ZapfDingbats. Contrary to apps like MS Word which could use fonts installed in the computer, FPDF won't recognize any fonts installed on the computer.<br />
<br />
FPDF could not directly use fonts from ttf files. instead, the ttf file should be converted into fpdf font library which consists of php files and a "z" file. We need to convert the ttf font into those library using a script provided by fpdf library in order to be able to use the fonts in our documents.<br />
In this tutorial, i'll explain how to convert, add and use custom font in FPDF library.<br />
For how to use fpdf library to generate pdf file in pdf, see <a href="http://www.asudahlah.com/2017/10/generate-printable-invoice-in-php-using.html" target="_blank">Generate Printable Invoice in PHP using FPDF Library</a>.<br />
<br />
<h3>
Create The FPDF Font Library by Converting TTF Files</h3>
To turn ttf font into php in order to use it in our generated pdf document, we can use makefont.php script inside makefont directory provided along with fpdf library distribution. You can download fpdf library <a href="http://www.fpdf.org/en/dl.php?v=17&f=zip" target="_blank">here</a>.<br />
First, extract the files into your web directory (htdocs or www). Inside, you'll find a folder named "makefont".<br />
Next, create a directory to place your ttf files (ie. customfont) which will be converted into fpdf font library.<br />
This is how my directories looks like:<br />
<span style="font-size: x-small;"><span style="font-family: "courier new" , "courier" , monospace;">htdocs/pdf</span></span><br />
<span style="font-size: x-small;"><span style="font-family: "courier new" , "courier" , monospace;"> |-fpdf17</span></span><br />
<span style="font-size: x-small;"><span style="font-family: "courier new" , "courier" , monospace;"> | |-doc</span></span><br />
<span style="font-size: x-small;"><span style="font-family: "courier new" , "courier" , monospace;"> | |-font</span></span><br />
<span style="font-size: x-small;"><span style="font-family: "courier new" , "courier" , monospace;"> | |-makefont</span></span><br />
<span style="font-size: x-small;"><span style="font-family: "courier new" , "courier" , monospace;"> | |-...</span></span><br />
<span style="font-size: x-small;"><span style="font-family: "courier new" , "courier" , monospace;"> |-customfont </span></span><br />
<span style="font-size: x-small;"><span style="font-family: "courier new" , "courier" , monospace;"> |-alienleagueii.ttf </span></span><br />
<span style="font-size: x-small;"><span style="font-family: "courier new" , "courier" , monospace;"> |-alienleagueiiital.ttf </span></span><br />
<span style="font-size: x-small;"><span style="font-family: "courier new" , "courier" , monospace;"> |-FREESCPT.TTF </span></span><br />
<span style="font-size: x-small;"><span style="font-family: "courier new" , "courier" , monospace;"> |-JOKERMAN.TTF </span></span><br />
I took 4 fonts as an example in this tutorial.<br />
Run your command line or terminal, then enter the customfont directory using cd command.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-I8ovhKBtmTo/Wf7SZ9XQu9I/AAAAAAAABJM/EZdMOIKl0ekZQIQ9HVqGRaazUvQgoeXugCLcBGAs/s1600/fpdf2-1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="86" data-original-width="414" height="66" src="https://2.bp.blogspot.com/-I8ovhKBtmTo/Wf7SZ9XQu9I/AAAAAAAABJM/EZdMOIKl0ekZQIQ9HVqGRaazUvQgoeXugCLcBGAs/s320/fpdf2-1.JPG" width="320" /></a></div>
Then run this command:<br />
<code>php <span style="background-color: #eeeeee;"><path to makefont.php></span>\makefont.php <span style="background-color: #eeeeee;"><font file name></span>.ttf</code><br />
<br />
Where <path to makefont.php> is either static or relative path to your makefont.php file, and the <font file name> is your font file name followed by .ttf extension. It's case sensitive.<br />
You'll get something like this.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-gTHj6nx94O8/Wf7TixM2ArI/AAAAAAAABJU/s8JwMuz_mbYWCt6NfzcsUpPIyuJKTlLXQCLcBGAs/s1600/fpdf2-2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="172" data-original-width="590" height="93" src="https://4.bp.blogspot.com/-gTHj6nx94O8/Wf7TixM2ArI/AAAAAAAABJU/s8JwMuz_mbYWCt6NfzcsUpPIyuJKTlLXQCLcBGAs/s320/fpdf2-2.jpg" width="320" /></a></div>
Repeat those steps for other fonts.<br />
<h3>
Add The Custom Fonts in FPDF Library</h3>
After you done converting the ttf files, go back to your customfont directory, you'll get some php and z file with font names in it.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-cznHCgD7JL4/Wf7VGAPGBFI/AAAAAAAABJo/ZFnBlO3SUDMot-yj5CMtKIZppT46AlW0ACLcBGAs/s1600/fpdf2-3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="317" data-original-width="521" height="194" src="https://1.bp.blogspot.com/-cznHCgD7JL4/Wf7VGAPGBFI/AAAAAAAABJo/ZFnBlO3SUDMot-yj5CMtKIZppT46AlW0ACLcBGAs/s320/fpdf2-3.jpg" width="320" /></a></div>
Copy those files, leaving aside the ttf files, into the font directory inside fpdf17 directory.<br />
You'll get something like this.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-UUjXDxjUsus/Wf7VjWkklzI/AAAAAAAABJs/COn2jpXz0_YgFn5UwSs50LZw2UPclf5AACLcBGAs/s1600/fpdf2-4.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="489" data-original-width="515" height="303" src="https://1.bp.blogspot.com/-UUjXDxjUsus/Wf7VjWkklzI/AAAAAAAABJs/COn2jpXz0_YgFn5UwSs50LZw2UPclf5AACLcBGAs/s320/fpdf2-4.jpg" width="320" /></a></div>
Then create a new php file for the pdf generator, include the fpdf.php library, and create a new FPDF object (Read <a href="http://www.asudahlah.com/2017/10/generate-printable-invoice-in-php-using.html" target="_blank">Generate Printable Invoice in PHP using FPDF Library</a> for further references on how to do that).<br />
<br />
To add the font into the documents, use AddFont method of fpdf library.<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #0f140f; color: #ff0007; font-style: italic; font-weight: bold;"><?php</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//add new freescript font</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">AddFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Freescript'</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086d2;">'FREESCPT.php'</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//add new jokerman font</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">AddFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Jokerman'</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086d2;">'JOKERMAN.php'</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//add alien league (regular)</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">AddFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Alien League'</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086d2;">'alienleagueii.php'</span><span style="color: white;">);</span>
</pre>
</div>
The first parameter is family name, second parameter is the font variant, third parameter is font's php file name.<br />
<br />
The font family name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.<br />
The font variant can be either of these strings:<br />
<ul>
<li>empty string: regular</li>
<li>B: bold</li>
<li>I: italic</li>
<li>BI or IB: bold italic</li>
</ul>
The default value is regular.<br />
<br />
For example, in this tutorial, i have 2 forms of Alien League fonts, alienleagueii.php for regular font, and alienleagueiiital.php for the italic font. I need to add both font separately with same family name but different variant and font files. So it should looks like this:<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #0f140f; color: #008800; font-style: italic;">//add alien league (regular)</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">AddFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Alien League'</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086d2;">'alienleagueii.php'</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//add alien league italic</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">AddFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Alien League'</span><span style="color: white;">,</span><span style="color: #0086d2;">'I'</span><span style="color: white;">,</span><span style="color: #0086d2;">'alienleagueiiital.php'</span><span style="color: white;">);</span>
</pre>
</div>
Please keep in mind that you need to add the fonts before you use it. So it's usually placed after fpdf object definition.<br />
<h3>
Use The Custom Font in PDF</h3>
To use the font, you can just call SetFont method with the family name and font variant like:<br />
$pdf->SetFont('Freescript','',36);<br />
<br />
Keep in mind that Freescript font used in this tutorial have no other variants beside regulars. So <code>$pdf->SetFont('Freescript','I',36);</code> would not work. But <code>$pdf->SetFont('Alien League','I',36);</code> will because we add the italic variants of Alien League font beforehand.<br />
<br />
Here's the complete scripts.<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #0f140f; color: #ff0007; font-style: italic; font-weight: bold;"><?php</span>
<span style="color: #fb660a; font-weight: bold;">require</span><span style="color: white;">(</span><span style="color: #0086d2;">'fpdf17/fpdf.php'</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span> <span style="color: white;">=</span> <span style="color: #fb660a; font-weight: bold;">new</span> <span style="color: white;">FPDF(</span><span style="color: #0086d2;">'P'</span><span style="color: white;">,</span><span style="color: #0086d2;">'mm'</span><span style="color: white;">,</span><span style="color: #0086d2;">'A4'</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//add new freescript font</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">AddFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Freescript'</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086d2;">'FREESCPT.php'</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//add new jokerman font</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">AddFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Jokerman'</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086d2;">'JOKERMAN.php'</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//add alien league (regular)</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">AddFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Alien League'</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086d2;">'alienleagueii.php'</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//add alien league italic</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">AddFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Alien League'</span><span style="color: white;">,</span><span style="color: #0086d2;">'I'</span><span style="color: white;">,</span><span style="color: #0086d2;">'alienleagueiiital.php'</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">AddPage</span><span style="color: white;">();</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//freescript font</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">SetFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Freescript'</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">36</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">190</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">20</span><span style="color: white;">,</span><span style="color: #0086d2;">'Freescript Font'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086d2;">'C'</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//jokerman font</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">SetFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Jokerman'</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">36</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">190</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">20</span><span style="color: white;">,</span><span style="color: #0086d2;">'Jokerman Font'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086d2;">'C'</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//alien league regular font</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">SetFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Alien League'</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">36</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">190</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">20</span><span style="color: white;">,</span><span style="color: #0086d2;">'Alien League Regular Font'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086d2;">'C'</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//alien league italic font</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">SetFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Alien League'</span><span style="color: white;">,</span><span style="color: #0086d2;">'I'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">36</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">190</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">20</span><span style="color: white;">,</span><span style="color: #0086d2;">'Alien League Italic Font'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086d2;">'C'</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Output</span><span style="color: white;">();</span>
</pre>
</div>
<br />
And here's what the results look like.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-bANI-d2ELfk/Wf7UA8oQ1LI/AAAAAAAABJg/KvT1RXpOPukP_-aFlHNa1LRDGJ7I_ZEzACEwYBhgL/s1600/fpdf2-5.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="616" data-original-width="1108" height="355" src="https://4.bp.blogspot.com/-bANI-d2ELfk/Wf7UA8oQ1LI/AAAAAAAABJg/KvT1RXpOPukP_-aFlHNa1LRDGJ7I_ZEzACEwYBhgL/s640/fpdf2-5.jpg" width="640" /></a></div>
<br />
For the video explanation of this tutorial, please <a href="https://www.youtube.com/watch?v=v63RjRqIzVE" target="_blank">watch this video</a>. And for other tutorials regarding PHP FPDF Library, please watch my youtube <a href="https://www.youtube.com/watch?v=XD8OOSwjMDs&list=PLHCU7DCP4jhxnxV4Ub4YDqWiZ061t183c" target="_blank">PHP FPDF Tutorial</a> Series.gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com5tag:blogger.com,1999:blog-7546034325993461816.post-92043295810720888632017-10-29T07:41:00.000-07:002018-05-28T22:45:23.539-07:00Generate Printable Invoice in PHP using FPDF library<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-hsaVPLrgosM/WfSjL-3r5hI/AAAAAAAABIU/tq44sNzM_UIu2I4Z_a9jBkPudnPzAYyKACLcBGAs/s1600/thumb.png" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" data-original-height="720" data-original-width="1280" height="112" src="https://4.bp.blogspot.com/-hsaVPLrgosM/WfSjL-3r5hI/AAAAAAAABIU/tq44sNzM_UIu2I4Z_a9jBkPudnPzAYyKACLcBGAs/s200/thumb.png" width="200" /></a></div>
<span id="goog_1203883912"></span><span id="goog_1203883913"></span>Nowadays, when almost everything is connected to internet. More people chooses web based application (or "webapp" in short) over desktop-based applications with various reasons but mostly because its flexibility and mobility. <br />
WebApp can become potentially powerful in computing power compared to stand alone application. Because the processing is done in server-side, even a low-end mobile devices can access robust information which requires complex calculation and can also manipulate it.<br />
But nothing is perfect, web app also have a lot of drawbacks. One of them is when it comes to physical document handling, such as document printing.<br />
<h3>
PDF generation in PHP</h3>
PDF generation in PHP gave a web-based application abilities to represent a printable, fixed- layout document to millimeter precision.<br />
Such as printable invoices, printable reports, printable manifests, and other printable documents.<br />
In short, PDF generation is a best method to represent a printable document, compared to any other method such as html, which is "dynamic layout" by nature.<br />
The down side is, we cannot alter the produced document and it depends on browser's PDF plugins whether to display it immediately on browser or just download it.<br />
Here's a tutorial of how to make a PDF invoice in PHP using FPDF class.<br />
You can download fpdf library package then extract it's content to your htdocs directory.<br />
This package contains fpdf php library, example tutorials, and documentation.<br />
FPDF has many methods to make and manipulate PDF content as you can see in its documentation <a href="http://www.fpdf.org/en/doc/" target="_blank">here</a>.<br />
<h3>
Make PDF Invoice using PHP FPDF library</h3>
First, you need to download the fpdf library <a href="http://www.fpdf.org/en/dl.php?v=17&f=zip" target="_blank">here</a>.<br />
Then extract it somewhere in your htdocs directory. Then make a php file as the pdf generator.<br />
In this example, i am going t use portrait, A4 size paper with mm measurement unit.<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #0f140f; color: #ff0007; font-style: italic; font-weight: bold;"><?php</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//call the FPDF library</span>
<span style="color: #fb660a; font-weight: bold;">require</span><span style="color: white;">(</span><span style="color: #0086d2;">'fpdf17/fpdf.php'</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//A4 width : 219mm</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//default margin : 10mm each side</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//writable horizontal : 219-(10*2)=189mm</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//create pdf object</span>
<span style="color: #fb660a;">$pdf</span> <span style="color: white;">=</span> <span style="color: #fb660a; font-weight: bold;">new</span> <span style="color: white;">FPDF(</span><span style="color: #0086d2;">'P'</span><span style="color: white;">,</span><span style="color: #0086d2;">'mm'</span><span style="color: white;">,</span><span style="color: #0086d2;">'A4'</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #ff0007; font-style: italic; font-weight: bold;">?></span><span style="color: white;"></span>
</pre>
</div>
<br />
Next, add a blank page using <span style="background-color: yellow;">$pdf->AddPage();</span> method. Then output the result using <span style="background-color: yellow;">$pdf->Output();</span> method.<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #0f140f; color: #ff0007; font-style: italic; font-weight: bold;"><?php</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//call the FPDF library</span>
<span style="color: #fb660a; font-weight: bold;">require</span><span style="color: white;">(</span><span style="color: #0086d2;">'fpdf17/fpdf.php'</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//A4 width : 219mm</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//default margin : 10mm each side</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//writable horizontal : 219-(10*2)=189mm</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//create pdf object</span>
<span style="color: #fb660a;">$pdf</span> <span style="color: white;">=</span> <span style="color: #fb660a; font-weight: bold;">new</span> <span style="color: white;">FPDF(</span><span style="color: #0086d2;">'P'</span><span style="color: white;">,</span><span style="color: #0086d2;">'mm'</span><span style="color: white;">,</span><span style="color: #0086d2;">'A4'</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//add new page</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">AddPage</span><span style="color: white;">();</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//output the result</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Output</span><span style="color: white;">();</span>
<span style="background-color: #0f140f; color: #ff0007; font-style: italic; font-weight: bold;">?></span><span style="color: white;"></span>
</pre>
</div>
<br />
You'll get something like:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-wUI1QdaMCPQ/WfSjY1GNnGI/AAAAAAAABIY/MrNu31nsD_Ezn66lKq1T3zKLGECZI1lvACLcBGAs/s1600/pdf1-1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="681" data-original-width="1318" height="165" src="https://3.bp.blogspot.com/-wUI1QdaMCPQ/WfSjY1GNnGI/AAAAAAAABIY/MrNu31nsD_Ezn66lKq1T3zKLGECZI1lvACLcBGAs/s320/pdf1-1.JPG" width="320" /></a></div>
<br />
Congratulations! you've just made a pdf file by yourself... Now what?...<br />
<h3>
Add content to pdf file with cells</h3>
Text content in pdf is written in cells (Imagine cells as a container). You can put text inside cells, and each cells has its own properties such as width, height, and border.<br />
Cells are stacked horizontally one after another from left to right and end with a cell which defined as a line ending by its parameter or until <span style="background-color: yellow;">$pdf->Ln();</span> method is invoked (imagine what "enter" button do when you're typing in text editor).<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-I_MF_k3TQTQ/WfXgUwTXDwI/AAAAAAAABIo/gprOewvc_VkfwZ3I0NK0gcEsIxb4kdCYQCLcBGAs/s1600/diagram1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="720" data-original-width="1280" height="180" src="https://2.bp.blogspot.com/-I_MF_k3TQTQ/WfXgUwTXDwI/AAAAAAAABIo/gprOewvc_VkfwZ3I0NK0gcEsIxb4kdCYQCLcBGAs/s320/diagram1.png" width="320" /></a></div>
Fpdf cell method have following parameters (taken from it's documentation <a href="http://www.fpdf.org/en/doc/cell.htm" target="_blank">here</a>):<br />
<code>Cell(<b>float</b> w [, <b>float</b> h [, <b>string</b> txt [, <b>mixed</b> border [, <b>int</b> ln [, <b>string</b> align [, <b>boolean</b> fill [, <b>mixed</b> link]]]]]]])</code><br />
w and h is for width and height.<br />
txt is the content.<br />
border to define the border. It could be 1 for all border, 0 for no border, and combination between L,T,R,B. Please refer to the doc for further explanation. what we need right now is 1 and 0.<br />
ln is line ending. 1 to end the line with this cell, otherwise 0.<br />
align is the text alignment L,C,R, no justified.<br />
as for fill and link, we don't need it right now so kindly refer to the docs.<br />
<h3>
Setting font properties in pdf</h3>
Before adding anything to the pdf, we should first set the font that will be used. Following is the SetFont method parameters taken from <a href="http://localhost/pdf/fpdf17/doc/setfont.htm" target="_blank">here</a>:<br />
<code>SetFont(<b>string</b> family [, <b>string</b> style [, <b>float</b> size]])</code><br />
family is the font such as 'Arial','Courier','Times', etc. Should be standard font or font defined by AddFont method (i'll explain it in another tutorial, meanwhile please watch <a href="https://www.youtube.com/watch?v=v63RjRqIzVE" target="_blank">this</a>).<br />
style is either one or combination between B,I,U or an empty string for none.<br />
size is the font size in points.<br />
<br />
Now we got the basics required to build our pdf invoice, let's move to the main point.<br />
<h3>
Make the printable PDF Invoice</h3>
Here is the codes for the invoice. Put this just after AddPage() and before Output().<br />
I included some comments as explanation in the code.<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #0f140f; color: #008800; font-style: italic;">//set font to arial, bold, 14pt</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">SetFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Arial'</span><span style="color: white;">,</span><span style="color: #0086d2;">'B'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">14</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//Cell(width , height , text , border , end line , [align] )</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">130</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'GEMUL APPLIANCES.CO'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">59</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'INVOICE'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">);</span><span style="background-color: #0f140f; color: #008800; font-style: italic;">//end of line</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//set font to arial, regular, 12pt</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">SetFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Arial'</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">12</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">130</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'[Street Address]'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">59</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">);</span><span style="background-color: #0f140f; color: #008800; font-style: italic;">//end of line</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">130</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'[City, Country, ZIP]'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">25</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'Date'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">34</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'[dd/mm/yyyy]'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">);</span><span style="background-color: #0f140f; color: #008800; font-style: italic;">//end of line</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">130</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'Phone [+12345678]'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">25</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'Invoice #'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">34</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'[1234567]'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">);</span><span style="background-color: #0f140f; color: #008800; font-style: italic;">//end of line</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">130</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'Fax [+12345678]'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">25</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'Customer ID'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">34</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'[1234567]'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">);</span><span style="background-color: #0f140f; color: #008800; font-style: italic;">//end of line</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//make a dummy empty cell as a vertical spacer</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">189</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">10</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">);</span><span style="background-color: #0f140f; color: #008800; font-style: italic;">//end of line</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//billing address</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">100</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'Bill to'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">);</span><span style="background-color: #0f140f; color: #008800; font-style: italic;">//end of line</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//add dummy cell at beginning of each line for indentation</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">10</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">90</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'[Name]'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">10</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">90</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'[Company Name]'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">10</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">90</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'[Address]'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">10</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">90</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'[Phone]'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//make a dummy empty cell as a vertical spacer</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">189</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">10</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">);</span><span style="background-color: #0f140f; color: #008800; font-style: italic;">//end of line</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//invoice contents</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">SetFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Arial'</span><span style="color: white;">,</span><span style="color: #0086d2;">'B'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">12</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">130</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'Description'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">25</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'Taxable'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">34</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'Amount'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">);</span><span style="background-color: #0f140f; color: #008800; font-style: italic;">//end of line</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">SetFont</span><span style="color: white;">(</span><span style="color: #0086d2;">'Arial'</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">12</span><span style="color: white;">);</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//Numbers are right-aligned so we give 'R' after new line parameter</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">130</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'UltraCool Fridge'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">25</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'-'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">34</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'3,250'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086d2;">'R'</span><span style="color: white;">);</span><span style="background-color: #0f140f; color: #008800; font-style: italic;">//end of line</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">130</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'Supaclean Diswasher'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">25</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'-'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">34</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'1,200'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086d2;">'R'</span><span style="color: white;">);</span><span style="background-color: #0f140f; color: #008800; font-style: italic;">//end of line</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">130</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'Something Else'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">25</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'-'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">34</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'1,000'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086d2;">'R'</span><span style="color: white;">);</span><span style="background-color: #0f140f; color: #008800; font-style: italic;">//end of line</span>
<span style="background-color: #0f140f; color: #008800; font-style: italic;">//summary</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">130</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">25</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'Subtotal'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">4</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'$'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">30</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'4,450'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086d2;">'R'</span><span style="color: white;">);</span><span style="background-color: #0f140f; color: #008800; font-style: italic;">//end of line</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">130</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">25</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'Taxable'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">4</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'$'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">30</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'0'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086d2;">'R'</span><span style="color: white;">);</span><span style="background-color: #0f140f; color: #008800; font-style: italic;">//end of line</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">130</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">25</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'Tax Rate'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">4</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'$'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">30</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'10%'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086d2;">'R'</span><span style="color: white;">);</span><span style="background-color: #0f140f; color: #008800; font-style: italic;">//end of line</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">130</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">''</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">25</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'Total Due'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">4</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'$'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">0</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$pdf</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">Cell</span><span style="color: white;">(</span><span style="color: #0086f7; font-weight: bold;">30</span> <span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">5</span><span style="color: white;">,</span><span style="color: #0086d2;">'4,450'</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086f7; font-weight: bold;">1</span><span style="color: white;">,</span><span style="color: #0086d2;">'R'</span><span style="color: white;">);</span><span style="background-color: #0f140f; color: #008800; font-style: italic;">//end of line</span>
</pre>
</div>
<br />
Save then refresh your browser. If everything is alright. you should have something like this.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-YXUgqbej1HM/WfXmpmC0ldI/AAAAAAAABI4/HU-aeLfZJ5EDcRC5OzykFlI65LNPxeoEgCLcBGAs/s1600/pdf1-2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="708" data-original-width="1365" height="165" src="https://3.bp.blogspot.com/-YXUgqbej1HM/WfXmpmC0ldI/AAAAAAAABI4/HU-aeLfZJ5EDcRC5OzykFlI65LNPxeoEgCLcBGAs/s320/pdf1-2.JPG" width="320" /></a></div>
Congratulation! You've just made a printable pdf invoice.<br />
<br />
If by any chance you're still confused after following this tutorial, please do enjoy <a href="https://www.youtube.com/watch?v=R32LF_pONNY" target="_blank">this video-version</a> of this tutorial. And if you want the source code, feel free to ask me in the comment section of aforementioned video.<br />
<br />gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com13tag:blogger.com,1999:blog-7546034325993461816.post-62302995290785429082017-04-14T20:02:00.000-07:002017-04-14T20:02:18.797-07:00Simple Ways of Getting Data from URL in PHP (Web Scraping)When you need to get data / content from a certain website url (ie. for scrapping, data fetching, or something like that). There are 2 simple ways you can use. By using built-in php function file_get_contents() and using cURL library.<span style="font-family: "Courier New", Courier, monospace;"><br /></span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-VKR_kU5R4RU/WPGJRbb42YI/AAAAAAAABGg/M5yBk28TK6grOrqYsCynCkqUxKXzRg2-ACLcB/s1600/asudahlah.com-phpwebscrap.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="263" src="https://1.bp.blogspot.com/-VKR_kU5R4RU/WPGJRbb42YI/AAAAAAAABGg/M5yBk28TK6grOrqYsCynCkqUxKXzRg2-ACLcB/s320/asudahlah.com-phpwebscrap.JPG" width="320" /></a></div>
<br />
file_get_contents is so far the most simple way of getting data, but it lacks options if compared to cURL library. So decide wisely which method suits your need. If you just need to get a website content, then file_get_contents is the best choice. But if you need to do something more, like setting user agent, timeout, response type, error handling, or to access HTTPS URLs. cURL is the most capable.<br />
<h3>
Get Content from URL using PHP file_get_contents()</h3>
Getting data using file_get_contens() is as simple as : <br />
<!-- HTML generated using hilite.me -->
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #0f140f; color: #ff0007; font-style: italic; font-weight: bold;"><?php</span>
<span style="color: #fb660a;">$url</span><span style="color: white;">=</span><span style="color: #0086d2;">"http://example.com"</span><span style="color: white;">;</span>
<span style="color: #fb660a;">$data</span><span style="color: white;">=file_get_contents(</span><span style="color: #fb660a;">$url</span><span style="color: white;">);</span>
<span style="color: #fb660a; font-weight: bold;">echo</span> <span style="color: #fb660a;">$data</span><span style="color: white;">;</span>
<span style="background-color: #0f140f; color: #ff0007; font-style: italic; font-weight: bold;">?></span><span style="color: white;"></span>
</pre>
</div>
It basically fetch anything from given address, whether it's a URL, or a file path. But for real-world implementation, every tcp connection does need some time to finish the request. In this case, the script would wait until the request processes finished (sending request, waiting, getting data) then continue the execution. If the connection is somehow failed, the script will keep continue so you will need to handle every possible outcome from the connection.<br />
<br />
<h3>
Get Content from URL using PHP DOMDocument class</h3>
Another way to fetch a web content in PHP is to use PHP DOMDocument class. In this method, we can directly process the retrieved document by DOM. The downside is, the content is only limited to HTML / XML. Usually, DOMDocument is already bundled in php so you don't have to install anything.<br />
Using DOMDocument is as simple as :<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #111111; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #0f140f; color: #ff0007; font-style: italic; font-weight: bold;"><?php</span>
<span style="color: #fb660a;">$dom</span> <span style="color: white;">=</span> <span style="color: #fb660a; font-weight: bold;">new</span> <span style="color: white;">DOMDocument();</span>
<span style="color: white;">libxml_use_internal_errors(</span><span style="color: #fb660a; font-weight: bold;">true</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$dom</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">loadHTMLFile</span><span style="color: white;">(</span><span style="color: #0086d2;">'http://example.com/'</span><span style="color: white;">);</span>
<span style="color: #fb660a;">$data</span> <span style="color: white;">=</span> <span style="color: #fb660a;">$dom</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">getElementById</span><span style="color: white;">(</span><span style="color: #0086d2;">"banner"</span><span style="color: white;">);</span>
<span style="color: #fb660a; font-weight: bold;">echo</span> <span style="color: #fb660a;">$data</span><span style="color: white;">-></span><span style="color: #ff0086; font-weight: bold;">nodeValue</span><span style="color: white;">.</span><span style="color: #0086d2;">"\n"</span>
<span style="background-color: #0f140f; color: #ff0007; font-style: italic; font-weight: bold;">?></span><span style="color: white;"></span>
</pre>
</div>
<br />
First, create a new DOMDocument object by <br />
<span style="background-color: #f4cccc;"><span style="font-family: "courier new" , "courier" , monospace;">$dom = new DOMDocument();</span></span><br />
<br />
Then load the HTML File using loadHTMLFile method with url as parameter.<br />
<span style="background-color: #f4cccc;"><span style="font-family: "courier new" , "courier" , monospace;">$dom->loadHTMLFile('http://example.com/'); </span></span><br />
<br />
Finally, access the DOM (you can read the docs <a href="http://php.net/manual/en/class.domdocument.php" target="_blank">here</a>)<br />
<span style="background-color: #f4cccc;"><span style="font-family: "courier new" , "courier" , monospace;">$dom->getElementById("elementId");</span></span><br />
<br />
In real world, not ALL html document is well formed. Whether it's an unclosed tags, mismatch brackets, malformed attribute-value pair, etc. Those will raise confuse DOMDocument causing it to throw an exception. To avoid that, we need to set<br />
<span style="background-color: #f4cccc;"><span style="font-family: "courier new" , "courier" , monospace;">libxml_use_internal_errors(true);</span></span><br />
to suppress libxml's internal errors.<br />
<br />
<h3>
Get Content from URL using PHPQuery class</h3>
If you are familiar with how jquery works, this method is the best for you. This class library is originally created by Tobiasz Cudnik. This class implement css3 selector based on jquery javascript library.<br />
By using PHPQuery class you can do something like:<br />
<span style="background-color: #cccccc;"><span style="font-family: "Courier New",Courier,monospace;">$doc['ul > li'] ->addClass('active-list');</span></span><br />
Feels familiar?<br />
<br />
You can find more example <a href="https://code.google.com/archive/p/phpquery/" target="_blank">here</a>.<br />
<br />
Beside those three above, there are many more alternatives methods to acquire data from webpages in PHP. IMHO, those three above is the best ways i am already familiar with.<br />
<br />gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com7tag:blogger.com,1999:blog-7546034325993461816.post-28124102882449994682016-12-23T18:56:00.000-08:002016-12-23T19:02:29.728-08:00PHPExcel Generate Excel Spreadsheet Files in PHP with Different FormatsPHPExcel is a powerful PHP library (class) which allows us to generate and read Excel spreadsheet file in PHP server-side. Developed by <a href="https://github.com/PHPOffice" rel="nofollow" target="_blank">PHPOffice team</a>. <br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-Q41EO6_gaFY/WF3josL_cHI/AAAAAAAABGE/6jsNbVJvDwobf7pu8TZElTy2oeFB2Ol7gCLcB/s1600/phpexcel-download-asudahlah.com.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="180" src="https://3.bp.blogspot.com/-Q41EO6_gaFY/WF3josL_cHI/AAAAAAAABGE/6jsNbVJvDwobf7pu8TZElTy2oeFB2Ol7gCLcB/s320/phpexcel-download-asudahlah.com.png" width="320" /></a></div>
<br />
Depending on the circumstances, generating an excel spreadsheet file as a reporting medium is a great way to present reporting data in offline, independent manner. Especially when dealing about printing and lots of numeric data such as financial report.<br />
<br />
Reading an excel file using PHP is also a good way to automate data entry. Especially when dealing with batch data entries. And on the end-user side, this method will also ease the user as the interface for entering the data is an excel spreadsheet file which most people are familiar with. And absolutely more agile when compared to web-interfaced forms.<br />
<br />
To use this library, simply download the library files <a href="https://github.com/PHPOffice/PHPExcel" target="_blank">here.</a><br />
<br />
Extract it anywhere in your web root directories. Call the <u>PHPExcel.php</u> file in your script and you're good to go.<br />
<br />
Here's a Hello World PHP script example to generate an excel file. <br />
I think the comments in pretty self-explanatory.
<!-- HTML generated using hilite.me --><div style="background: #111111; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #ff0007; font-weight: bold; font-style: italic; background-color: #0f140f"><?php</span>
<span style="color: #fb660a; font-weight: bold">require_once</span> <span style="color: #0086d2">'Classes/PHPExcel.php'</span><span style="color: #ffffff">;</span>
<span style="color: #008800; font-style: italic; background-color: #0f140f">//create PHPExcel object</span>
<span style="color: #fb660a">$excel</span> <span style="color: #ffffff">=</span> <span style="color: #fb660a; font-weight: bold">new</span> <span style="color: #ffffff">PHPExcel();</span>
<span style="color: #008800; font-style: italic; background-color: #0f140f">//insert some data to PHPExcel object</span>
<span style="color: #fb660a">$excel</span><span style="color: #ffffff">-></span><span style="color: #ff0086; font-weight: bold">setActiveSheetIndex</span><span style="color: #ffffff">(</span><span style="color: #0086f7; font-weight: bold">0</span><span style="color: #ffffff">)</span>
<span style="color: #ffffff">-></span><span style="color: #ff0086; font-weight: bold">setCellValue</span><span style="color: #ffffff">(</span><span style="color: #0086d2">'A1'</span><span style="color: #ffffff">,</span><span style="color: #0086d2">'Hello'</span><span style="color: #ffffff">)</span>
<span style="color: #ffffff">-></span><span style="color: #ff0086; font-weight: bold">setCellValue</span><span style="color: #ffffff">(</span><span style="color: #0086d2">'B1'</span><span style="color: #ffffff">,</span><span style="color: #0086d2">'World'</span><span style="color: #ffffff">);</span>
<span style="color: #008800; font-style: italic; background-color: #0f140f">//redirect to browser (download) instead of saving the result as a file</span>
<span style="color: #008800; font-style: italic; background-color: #0f140f">//this is for MS Office Excel 2007 xlsx format</span>
<span style="color: #ffffff">header(</span><span style="color: #0086d2">'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'</span><span style="color: #ffffff">);</span>
<span style="color: #ffffff">header(</span><span style="color: #0086d2">'Content-Disposition: attachment; filename="test.xlsx"'</span><span style="color: #ffffff">);</span>
<span style="color: #008800; font-style: italic; background-color: #0f140f">//this is for MS Office Excel 2003 xls format</span>
<span style="color: #008800; font-style: italic; background-color: #0f140f">//header('Content-Type: application/vnd.ms-excel');</span>
<span style="color: #008800; font-style: italic; background-color: #0f140f">//header('Content-Disposition: attachment; filename="test.xlsx"');</span>
<span style="color: #ffffff">header(</span><span style="color: #0086d2">'Cache-Control: max-age=0'</span><span style="color: #ffffff">);</span>
<span style="color: #008800; font-style: italic; background-color: #0f140f">//write the result to a file</span>
<span style="color: #008800; font-style: italic; background-color: #0f140f">//for excel 2007 format</span>
<span style="color: #fb660a">$file</span> <span style="color: #ffffff">=</span> <span style="color: #ffffff">PHPExcel_IOFactory::</span><span style="color: #ff0086; font-weight: bold">createWriter</span><span style="color: #ffffff">(</span><span style="color: #fb660a">$excel</span><span style="color: #ffffff">,</span><span style="color: #0086d2">'Excel2007'</span><span style="color: #ffffff">);</span>
<span style="color: #008800; font-style: italic; background-color: #0f140f">//for excel 2003 format</span>
<span style="color: #008800; font-style: italic; background-color: #0f140f">//$file = PHPExcel_IOFactory::createWriter($excel,'Excel5');</span>
<span style="color: #008800; font-style: italic; background-color: #0f140f">//output to php output instead of filename</span>
<span style="color: #fb660a">$file</span><span style="color: #ffffff">-></span><span style="color: #ff0086; font-weight: bold">save</span><span style="color: #ffffff">(</span><span style="color: #0086d2">'php://output'</span><span style="color: #ffffff">);</span>
<span style="color: #ff0007; font-weight: bold; font-style: italic; background-color: #0f140f">?></span><span style="color: #ffffff"></span>
</pre></div>
<br />
You can choose in what format the excel file will be generated by uncomment/comment the headers and IOFactory line.<br />
<br />
Basically, what needs to be change to save it in different formats is:<br />
for Excel 2007 or above<br />
header('Content-Type: <b>application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</b>');<br />
header('Content-Disposition: attachment; filename="test.<b>xlsx</b>"');<br />
$file = PHPExcel_IOFactory::createWriter($excel,'<b>Excel2007</b>');<br />
<br />
and for Excel 2003<br />
header('Content-Type: <b>application/vnd.ms-excel</b>');<br />
header('Content-Disposition: attachment; filename="test.<b>xls</b>"'); <br />
$file = PHPExcel_IOFactory::createWriter($excel,'<b>Excel5</b>');<br />
<br />
And here's the video if you need a step-by-step explanation.
<br />
<br />
Part #1 How to install and test php excel (basic usage).<br />
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen="" class="YOUTUBE-iframe-video" data-thumbnail-src="https://i.ytimg.com/vi/EcISZmyh9hQ/0.jpg" frameborder="0" height="266" src="https://www.youtube.com/embed/EcISZmyh9hQ?feature=player_embedded" width="320"></iframe></div>
<br />
Part #2 Download PHP Generated Excel File.<br />
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen="" class="YOUTUBE-iframe-video" data-thumbnail-src="https://i9.ytimg.com/vi/Pl6oyU46lCQ/default.jpg?sqp=CNjE98IF&rs=AOn4CLCRVkX8qjSG5w4Z4XgmqtpJPE5BUw" frameborder="0" height="266" src="https://www.youtube.com/embed/Pl6oyU46lCQ?feature=player_embedded" width="320"></iframe></div>
<br />gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com175tag:blogger.com,1999:blog-7546034325993461816.post-45859229964357236432016-12-16T02:06:00.001-08:002018-06-25T00:55:51.408-07:00Conditional Formatting in PHPExcel<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-e9elICf6k6U/WFO8PyVyR3I/AAAAAAAABF0/ECkplCUe3MwaIuo-ZjWQUWwLvmiycljLwCLcB/s1600/php-excel-1-asudahlah.com.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="258" src="https://1.bp.blogspot.com/-e9elICf6k6U/WFO8PyVyR3I/AAAAAAAABF0/ECkplCUe3MwaIuo-ZjWQUWwLvmiycljLwCLcB/s320/php-excel-1-asudahlah.com.jpg" width="320" /></a></div>
Doing conditional formatting in PHPExcel is pretty easy. First you have to
define the formatting. Then apply it to a cell.<br />
<br />
I hope this snippet could be useful.
<!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #4c8317"><?php</span>
...
<span style="color: #aa0000">$objConditional</span> = <span style="color: #0000aa">new</span> PHPExcel_Style_Conditional();
<span style="color: #aa0000">$objConditional</span>-><span style="color: #1e90ff">setConditionType</span>(PHPExcel_Style_Conditional::<span style="color: #1e90ff">CONDITION_CELLIS</span>)
-><span style="color: #1e90ff">setOperatorType</span>(PHPExcel_Style_Conditional::<span style="color: #1e90ff">OPERATOR_LESSTHAN</span>)
-><span style="color: #1e90ff">addCondition</span>(<span style="color: #aa5500">'B2'</span>)
-><span style="color: #1e90ff">getStyle</span>()
-><span style="color: #1e90ff">applyFromArray</span>(
<span style="color: #0000aa">array</span>(
<span style="color: #aa5500">'font'</span>=><span style="color: #0000aa">array</span>(
<span style="color: #aa5500">'color'</span>=><span style="color: #0000aa">array</span>(<span style="color: #aa5500">'argb'</span>=><span style="color: #aa5500">'FF000000'</span>)
),
<span style="color: #aa5500">'fill'</span>=><span style="color: #0000aa">array</span>(
<span style="color: #aa5500">'type'</span> =>PHPExcel_Style_Fill::<span style="color: #1e90ff">FILL_SOLID</span>,
<span style="color: #aa5500">'startcolor'</span> =><span style="color: #0000aa">array</span>(<span style="color: #aa5500">'argb'</span> => <span style="color: #aa5500">'FFFF0000'</span>),
<span style="color: #aa5500">'endcolor'</span> =><span style="color: #0000aa">array</span>(<span style="color: #aa5500">'argb'</span> => <span style="color: #aa5500">'FFFF0000'</span>)
)
)
);
<span style="color: #aa0000">$conditionalStyles</span> = <span style="color: #aa0000">$objPHPExcel</span>-><span style="color: #1e90ff">getActiveSheet</span>()-><span style="color: #1e90ff">getStyle</span>(<span style="color: #aa5500">'C2'</span>)-><span style="color: #1e90ff">getConditionalStyles</span>();
<span style="color: #00aaaa">array_push</span>(<span style="color: #aa0000">$conditionalStyles</span>,<span style="color: #aa0000">$objConditional</span>);
<span style="color: #aa0000">$objPHPExcel</span>-><span style="color: #1e90ff">getActiveSheet</span>()-><span style="color: #1e90ff">getStyle</span>(<span style="color: #aa5500">'C2'</span>)-><span style="color: #1e90ff">setConditionalStyles</span>(<span style="color: #aa0000">$conditionalStyles</span>);
</pre></div>
<br />
B2 is the reference cell, and C2 is the cell for conditional formating to be applied to.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-FisFUmMom28/WFO8OVrKt2I/AAAAAAAABFw/zK3UypmBQtglFCNAV-jC3i2Z4L5jpIdagCLcB/s1600/php-excel-1-asudahlah.com-2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="165" src="https://1.bp.blogspot.com/-FisFUmMom28/WFO8OVrKt2I/AAAAAAAABFw/zK3UypmBQtglFCNAV-jC3i2Z4L5jpIdagCLcB/s320/php-excel-1-asudahlah.com-2.jpg" width="320" /></a></div>
<br />
By the way, not everything went smooth when i did this, because the "fill" style doesn't works while the "font" style works well. I spend hour to scour the documentation, and search every possible keywords in google before i finally found what the problem is.<br />
<blockquote class="tr_bq">
If you want to use SOLID fill, you need to use 'startcolor' instead of 'color'.</blockquote>
That's it. Simple matter but cost me my worthy minutes.<br />
P.S: If you want to use textual conditional formatting insetad of numeric data. Check out <a href="https://www.asudahlah.com/2016/12/conditional-formatting-in-phpexcel.html?showComment=1529913092981#c5739090253247135119">Krazal's comment below</a>.
gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com2tag:blogger.com,1999:blog-7546034325993461816.post-43811377879490594872016-11-21T22:08:00.000-08:002016-11-21T22:09:26.690-08:00How to set up XAMPP-bundled php path in windows 7Most student, teacher, or even web developer used bundled webserver applications for their projects such as xampp or uniserv.<br />
<br />
When using native php installation, we may use 'php' command directly from command prompt / terminal to use php cli. But if we are using xampp, because the php executable path is not defined in the system environment by default, we need to call php executable bin by complete path such as <span style="font-family: "courier new" , "courier" , monospace;">'C:\xampp\php\php.exe'</span> everytime to run php cli which is very annoying.<br />
<br />
To avoid doing so, we can manually define php executable path in windows system environment You can do that by following these steps.<br />
<br />
First, find out where your php executable is located. In XAMPP, it's in <span style="font-family: "courier new" , "courier" , monospace;">C:\xampp\php</span> by default. You may copy the path from address bar to avoid typo.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-O4ZWa3LW1r4/WDPcq0D-GJI/AAAAAAAABFI/raQzaSbDGqETEG-5PnAApGDMDKmHBkNzwCLcB/s1600/Screenshot%2Bfrom%2B2016-11-22%2B12%253A28%253A33.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="96" src="https://4.bp.blogspot.com/-O4ZWa3LW1r4/WDPcq0D-GJI/AAAAAAAABFI/raQzaSbDGqETEG-5PnAApGDMDKmHBkNzwCLcB/s320/Screenshot%2Bfrom%2B2016-11-22%2B12%253A28%253A33.png" width="320" /></a></div>
Next, open your Environment variables by going through : Left click My Computer, Properties, Advanced system settings, and then click Environment Variables.<br />
<br />
Choose PATH from System Variables, Edit...<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-_M2GSc2HmD0/WDPejVZbRnI/AAAAAAAABFQ/cxCsKf4tiicLPTNOHC3F7P3OtOJvhuEMQCLcB/s1600/Screenshot%2Bfrom%2B2016-11-22%2B12%253A29%253A05.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="244" src="https://2.bp.blogspot.com/-_M2GSc2HmD0/WDPejVZbRnI/AAAAAAAABFQ/cxCsKf4tiicLPTNOHC3F7P3OtOJvhuEMQCLcB/s320/Screenshot%2Bfrom%2B2016-11-22%2B12%253A29%253A05.png" width="320" /></a></div>
Then at the end of Variable Value, Add (DO NOT REPLACE ANYTHING, just append at the end) a semicolon, followed by your php executable path so it will looks like:<br />
<span style="font-family: "courier new" , "courier" , monospace;"><anotherEntries>;C:\xampp\php</span><br />
Every entries are separated by semicolon<br />
<br />
Hit OK three times to close all windows.<br />
<br />
Open your command prompt, and try command:<br />
<span style="font-family: "courier new" , "courier" , monospace;">php -v</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-okDOVJm0tuc/WDPgChD94zI/AAAAAAAABFY/wC5WFxXic1wiBlkCUUahV1duwRC3VKzKwCLcB/s1600/Screenshot%2Bfrom%2B2016-11-22%2B12%253A29%253A38.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="163" src="https://4.bp.blogspot.com/-okDOVJm0tuc/WDPgChD94zI/AAAAAAAABFY/wC5WFxXic1wiBlkCUUahV1duwRC3VKzKwCLcB/s320/Screenshot%2Bfrom%2B2016-11-22%2B12%253A29%253A38.png" width="320" /></a></div>
<br />
your current php version should be displayed. which means from now on you can directly call 'php' from your command prompt to use php cli.<br />
<br />
That concludes this tutorial. You can watch the video below to understand this tutorial better.<br />
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen="" class="YOUTUBE-iframe-video" data-thumbnail-src="https://i.ytimg.com/vi/51IlfNzZVGo/0.jpg" frameborder="0" height="266" src="https://www.youtube.com/embed/51IlfNzZVGo?feature=player_embedded" width="320"></iframe></div>
<br />
<br />
<br />gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com2tag:blogger.com,1999:blog-7546034325993461816.post-6210546365595429972016-10-04T22:56:00.000-07:002016-11-21T20:52:56.190-08:00Mikrotik | How to Solve Blocked DNS Port (53) Under Hotspot NetworkAfter i implement hotspot network in my workplace, everything went smooth. But after awhile, a problem arise. We, anyone behind hotspot, could not resolve some DNS, especially the one hosted by google such as customized blogger domain like this blog, asudahlah.com.<br />
<br />
I can ping asudahlah.com using mikrotik terminal in winbox, but i cannot nslookup it in my laptop and my coworker's laptop. I also try with another domain name, some of them get thru, some didn't. My DNS flow goes like this:<br />
<br />
Client (DNS=192.168.68.1)-------->(IP=192.168.68.1) Mikrotik (DNS=8.8.8.8)------->internet.<br />
<br />
After some trial and error, i found what the problem is. Mikrotik's hotspot dynamically created a NAT rules which redirect DNS port TCP 53 and UDP 53 in order for the captive portal to work.<br />
<br />
Here's a solution which works for my case. By bypassing TCP and UDP port 53. Here's the configuration:<br />
<br />
<span style="font-size: small;"><span style="font-family: "courier new" , "courier" , monospace;">/ip firewall nat<br />add action=accept chain=pre-hotspot disabled=no dst-port=53 protocol=udp<br />add action=accept chain=pre-hotspot disabled=no dst-port=53 protocol=tcp<br />/ip hotspot walled-garden ip<br />add action=accept disabled=no dst-port=53 protocol=udp<br />add action=accept disabled=no dst-port=53 protocol=tcp</span></span><br />
<br />
Well, that's it. Now i can access all domain.<br />
<br />
I hope it helps anyone having the same problem.gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com5tag:blogger.com,1999:blog-7546034325993461816.post-50762384402198586612016-04-19T20:32:00.000-07:002016-11-21T21:25:22.901-08:00How to check battery status in ubuntu using command terminalOn linux, we can find out information and status of the battery through the terminal. Here's a very short tutorial on how to see the status of the battery through terminal with upower command, and a tips to make it lot easier.<br />
<br />
Upower is an abstraction for enumerating devices, power devices, activity monitor and update the query history and statistics. Created by freedesktop.org. Information and complete documentation can be visited at http://upower.freedesktop.org/.<br />
<br />
To view battery information, the first is to search path for device battery with the command:<br />
<br />
upower -e<br />
<br />
Then some device path will show up like example below:<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">/org/freedesktop/UPower/devices/line_power_ADP0
/org/freedesktop/UPower/devices/battery_BAT0
/org/freedesktop/UPower/devices/DisplayDevice
</pre>
</div>
<br />
<blockquote>
If it the device path list does not appear like above, but instead the error message "command not found" that means upower is not installed (Which is very rare occasion in <a class="zem_slink" href="http://en.wikipedia.org/wiki/Linux" rel="wikipedia" target="_blank" title="Linux">linux OS</a>). Please install first upower with command:<br />
sudo apt-get install upower<br />
for ubuntu, debian, mint and it's kind.</blockquote>
<br />
<br />
<br />
Each of 3 paths above pointing to the power devices, such as, in the above example is the line power (<a class="zem_slink" href="http://en.wikipedia.org/wiki/AC_adapter" rel="wikipedia" target="_blank" title="AC adapter">AC adapter</a>), battery, and display device.<br />
<br />
After we figure out the path for the battery, in the case above, it's /org/freedesktop/UPower/devices/battery_BAT0, then we can check the battery status with the command:<br />
<br />
<pre>upower -i /org/freedesktop/UPower/devices/battery_BAT0</pre>
<br />
It will output some information like below:<br />
<br /><!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;background: rgb(255, 255, 255) none repeat scroll 0% 0%; border-style: solid; border-color: gray; -moz-border-top-colors: none; -moz-border-right-colors: none; -moz-border-bottom-colors: none; -moz-border-left-colors: none; border-image: none; overflow: auto; padding: 0.2em 0.6em; width: auto; border-width: 0.1em 0.1em 0.1em 0.8em;"><pre style="margin: 0; line-height: 125%"><span style="color: #888888">native-path: BAT0</span>
<span style="color: #888888">vendor: <a class="zem_slink" title="Laptop" href="http://en.wikipedia.org/wiki/Laptop" target="_blank" rel="wikipedia">NOTEBOOK</a></span>
<span style="color: #888888">model: BAT</span>
<span style="color: #888888">serial: 0001</span>
<span style="color: #888888">power supply: yes</span>
<span style="color: #888888">updated: Sat 29 Aug 2015 10:51:07 PM WIB (107 seconds ago)</span>
<span style="color: #888888">has history: yes</span>
<span style="color: #888888">has statistics: yes</span>
<span style="color: #888888">battery</span>
<span style="color: #888888">present: yes</span>
<span style="color: #888888">rechargeable: yes</span>
<span style="color: #888888">state: discharging</span>
<span style="color: #888888">warning-level: none</span>
<span style="color: #888888"><a class="zem_slink" title="Energy" href="http://en.wikipedia.org/wiki/Energy" target="_blank" rel="wikipedia">energy</a>: 19.9911 Wh</span>
<span style="color: #888888">energy-empty: 0 Wh</span>
<span style="color: #888888">energy-full: 31.3797 Wh</span>
<span style="color: #888888">energy-full-design: 48.84 Wh</span>
<span style="color: #888888">energy-rate: 12.9156 W</span>
<span style="color: #888888">voltage: 14.8 V</span>
<span style="color: #888888">time to empty: 1.5 hours</span>
<span style="color: #888888">percentage: 63%</span>
<span style="color: #888888">capacity: 64.25%</span>
<span style="color: #888888">technology: <a class="zem_slink" title="Lithium-ion battery" href="http://en.wikipedia.org/wiki/Lithium-ion_battery" target="_blank" rel="wikipedia">lithium-ion</a></span>
<span style="color: #888888">icon-name: 'battery-full-symbolic'</span>
<span style="color: #888888">History (charge):</span>
<span style="color: #888888">1440863467 63.000 discharging</span>
<span style="color: #888888">History (rate):</span>
<span style="color: #888888">1440863467 12.916 discharging</span>
</pre></div>
<br />
<br />
From the information above, we know that my laptop isn't connected to a charger / power source (state = discharging). Battery energy (energy) 19.9911 Wh or 63%.in percentage.<br />
<br />
Full battery (energy-full) 31.3797 Wh. Which means my battery life have been degraded, and could only be charged until it reach 31.3797 Wh, which is, by design, supposed to be (energy-full-design) 48.84 Wh. Its max capacities (capacity) has been reduced from the original 64.25% (new).<br />
<br />
You may experiment and observe other information.<br />
<h2>
How to make a shortcut to check laptop batteries in terminal</h2>
<br />
Now to make it even easier, we can create a command alias/shortcut for the above commands.<br />
<br />
It's as simple as editiing the .bashrc file. in your home directory:<br />
<br />
nano ~/.bashrc<br />
<br />
If the file does not exist (empty), try to replace it with the file. bash_profile or. profile.<br />
<br />
And then on top of the lines, add the line:<br />
<br />
<br />
<pre>alias battery='upower -i /org/freedesktop/UPower/devices/battery_BAT0'</pre>
<br />
<br />
change 'battery' onto whatever word you like without any spaces.<br />
<br />
Save and execute:<br />
<br />
<br />
<pre>source ~/.bashrc</pre>
<br />
to reload your .bashrc or .bash_profile flle.<br />
<br />
Then try your newly defined alias.<br />
<br />
<img alt="" class="alignnone" src="http://chs.asudahlah.com/wp-content/uploads/2015/08/baterai-300x262.jpg" height="262" width="300" />gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com0tag:blogger.com,1999:blog-7546034325993461816.post-16132119741323578312016-04-18T21:54:00.000-07:002016-06-01T17:42:30.607-07:00File Transfer over network using SSH and File ManagerThere are many ways to transfer files over the network on <a class="zem_slink" title="Ubuntu (operating system)" href="http://www.ubuntu.com" target="_blank" rel="homepage">linux ubuntu</a>, by using samba server, http server, ftp, sftp, and many more. One of the most accessible among them and the one I will discuss this time is to use <a class="zem_slink" title="Secure file transfer program" href="http://en.wikipedia.org/wiki/Secure_file_transfer_program" target="_blank" rel="wikipedia">SFTP</a> (<a class="zem_slink" title="Secure Shell" href="http://en.wikipedia.org/wiki/Secure_Shell" target="_blank" rel="wikipedia">SSH</a> <a class="zem_slink" title="File Transfer Protocol" href="http://en.wikipedia.org/wiki/File_Transfer_Protocol" target="_blank" rel="wikipedia">File Transfer Protocol</a>). in other words, transfer files through SSH.<br/><br/>There are 3 basic requirements for this method:<br/><ol><br/> <li>The network is set up in such a way so as to allow each computer to ping.</li><br/> <li><a class="zem_slink" title="Comparison of SSH servers" href="http://en.wikipedia.org/wiki/Comparison_of_SSH_servers" target="_blank" rel="wikipedia">SSH server</a> on the computer that will be accessed must be installed, enabled, and can be accessed/login from another computer. For a guide to installing SSH Server, you can read in the article how to enable and configure SSH Server on ubuntu</li><br/> <li>Applications such as file manager nautilus, nemo, thunar, etc. (in this example I use the default ubuntu <a class="zem_slink" title="Nautilus (file manager)" href="http://live.gnome.org/Nautilus" target="_blank" rel="homepage">Nautilus</a>) already installed on the client computer to access.</li><br/></ol><br/><h2>How to connect file manager to SFTP</h2><br/><h3>Method 1 of 2</h3><br/>Open file manager (in this example, nautilus).<br/><br/>Then click file menu or context menu (gear/cog icon on top-right of nautilus window). Then choose "Connect to server..." like in the picture below.<br/><br/><img class="alignnone" src="http://asudahlah.com/wp-content/uploads/2015/05/sftp0-300x297.png" alt="" width="300" height="297" /><br/><br/>Then in the "connect to server" dialog, enter the destination address along with the sftp Protocol. In the example picture below I used the address 192.168.6.2, so the address is: sftp://192.168.6.2<br/><br/><img class="alignnone" src="http://asudahlah.com/wp-content/uploads/2015/05/sftp3-300x266.png" alt="" width="300" height="266" /><br/><blockquote>If after clicking "Connect" nothing happens, maybe the addresses that you entered could not be accessed. Make sure that the ip address is ping-able and SSH server is running on that computer.<br/><br/>When the destination computer's SSH server using tcp port other than the default port of the ssh (port 22), then add the <a class="zem_slink" title="Port (computer networking)" href="http://en.wikipedia.org/wiki/Port_%28computer_networking%29" target="_blank" rel="wikipedia">port number</a> after the colon (:) for example sftp://192.168.6.2:1234</blockquote><br/><img class="alignnone" src="http://asudahlah.com/wp-content/uploads/2015/05/sftp21-300x289.png" alt="" width="300" height="289" /><br/><br/>Then click the "Connect" button. And wait until the login proccess is success, the nautilus will open the home directory of the user that you use to login.<br/><blockquote>To enter another directory such as/,/var/www,/etc, press "Ctrl + L", and then in the address / location bar, delete addresses that point to the home directory. Example: sftp://192.168.6.2/home/asudahlah becomes sftp://192.168.6.2/or sftp://192.168.6.2/var/www</blockquote><br/> <br/><h3>Method 2 of 2</h3><br/>This way more simple than the first. 😀 you just need to press "Ctrl + L", then enter the <a class="zem_slink" title="IP address" href="http://en.wikipedia.org/wiki/IP_address" target="_blank" rel="wikipedia">IP address</a> of the destination and its protocol such as sftp://192.168.6.2/, and then press enter. And the login dialog will appear and you can read the next steps in the method 1. 😀 that's it.<br/><h2>P.S</h2><br/>Just like FTP, these methods uses client-server. Where the client can access the server, but not vice versa. However, you can delete, create, transmit, and retrieve files depending on the access rights / permission of the user you used when logged in.<br/><br/>In file manager, a connection that you create will just be treated as a directory as if in mount and is usually displayed on the sidebar.gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com3tag:blogger.com,1999:blog-7546034325993461816.post-1315759717232392262016-04-18T21:32:00.000-07:002016-06-01T17:42:30.596-07:00How to fix MS office crash in wine caused by my network placesOn some occasions users of <a class="zem_slink" title="Microsoft Office" href="http://en.wikipedia.org/wiki/Microsoft_Office" target="_blank" rel="wikipedia">MS Office</a> in wine get annoyed because Ms Office crash or not responding when they're about to save their works, resulting in loss of all the works. And mostly it caused by accidentally click "<a class="zem_slink" title="My Network Places" href="http://en.wikipedia.org/wiki/My_Network_Places" target="_blank" rel="wikipedia">my network places</a>" location in save dialog.<br/><br/><img class="aligncenter" src="http://asudahlah.com/wp-content/uploads/2015/12/Screenshot-from-2015-12-04-081744.jpg" alt="" width="280" height="161" /><br/><br/>And it's very annoying because the whole work that is already done is lost because it could not be saved. We may be able to recover it. but still, it's annoying.<br/><br/>When in windows, that won't be a problem. But in wine, because basically we are not running the windows operating system so that the path to my network places leads to nowhere, causing infinite loading when you access it.<br/><br/>The only way (IMHO) to fix the problem is to not display that problematic button at all in order to avoid "accidental click".<br/><h2>How to hide my network places icon in MS Office save dialog</h2><br/><ol><br/> <li>The first step is to open the <a class="zem_slink" title="Windows Registry" href="http://en.wikipedia.org/wiki/Windows_Registry" target="_blank" rel="wikipedia">registry editor</a>, do I run the winetrick application.</li><br/> <li>Then select the menu "select the default wineprefix".</li><br/> <li>Choose "run regedit".</li><br/> <li>After the registry editor, go to:<br/>HKEY_CURRENT_USER\<a class="zem_slink" title="Application software" href="http://en.wikipedia.org/wiki/Application_software" target="_blank" rel="wikipedia">Software</a>\<a class="zem_slink" title="Microsoft" href="http://maps.google.com/maps?ll=47.6395972222,-122.12845&spn=0.01,0.01&q=47.6395972222,-122.12845 (Microsoft)&t=h" target="_blank" rel="geolocation">Microsoft</a>\Office\12.0\Common\Open Find\Places\StandardPlacesNote: The colored green may be different, depending on the version of office that you have. just select the number of the most high.</li><br/> <li>Then on each key that exists in the StandardPlaces, change the DWORD registry value of "Show" to 0 by right click on the registry in the right pane, and then select modify, change the Value to 0, and then OK. (If there are any registry named "View", change also the value to 0).<br/><img class="alignnone" src="http://asudahlah.com/wp-content/uploads/2015/12/Screenshot-from-2015-12-04-082730-300x155.jpg" alt="" width="300" height="155" /><br/>If no registry DWORD named "Show", please create your own way right dipanel right click and select New DWORD Value >, then give the name of the "Show" without the quotation marks and then enter. By default its Value is 0.</li><br/> <li>Repeat step 5 and 6 for key Recent, Publishing, My Computer and Desktop</li><br/> <li>Make sure it is correct, and then exit the registry.</li><br/> <li>Please try opening the application MS Office again, and then open the file. It should be in the left panel there is no my network places.</li><br/></ol><br/>This concludes the short tutorials of how to fix MS office crash in wine caused by my network places.<br/><br/>Hope it could help anyone facing the same problem.gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com2tag:blogger.com,1999:blog-7546034325993461816.post-86357121994137630542016-04-12T22:43:00.000-07:002016-06-01T17:42:30.585-07:00How to fix "error Could not get lock /var/lib/dpkg/lock resource
temporarily unavailable" in ubuntuOften when we do the apt commands, whether it be to install, update, or uninstall/remove. We'll likely encountered a problem, ranging from a mere trivial to an annoying error which makes it impossible to continue the process. One of the most common <a class="zem_slink" href="http://en.wikipedia.org/wiki/Error_message" title="Error message" rel="wikipedia" target="_blank">error message</a> is:<br/><br/>[code lang="bash"] E: Could not get lock/var/lib/dpkg/lock-open (11 Resource be temporarily unavailable)<br/>E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it? [/code]<br/><br/>Generally this is because we forcibly stop or interrupt the process when performing the <a class="zem_slink" href="http://wiki.debian.org/Apt" title="Advanced Packaging Tool" rel="homepage" target="_blank">apt-get</a> command. Usually with <a class="zem_slink" href="http://en.wikipedia.org/wiki/Control-C" title="Control-C" rel="wikipedia" target="_blank">Ctrl + C</a> or interrupt (halt). It causes the process to suddenly stop and the apt didn't manage to open the lock that prevents the <a class="zem_slink" href="http://en.wikipedia.org/wiki/APT_process" title="APT process" rel="wikipedia" target="_blank">apt process</a> from running simultaneously.<br/><br/>Basically, the apt is not allowed to run more than one process, because it can mess up the dependencies. So it requires a lock to make sure that there is only 1 process running. When we run the command apt, apt will then automatically create a lock, and when we try to run another apt command, the apt will check if there is a lock, if any, then the second apt process will stop and display the error message above.<br/><br/>Sometimes, when the apt process is forced to stop or interrupted (halt), the process had not previously delete the apt lock. So the apt command we try to run will recognize that there is an apt processes running, despite there isn't. And in the end, the apt will terminate with error.<br/><br/>The fix is pretty easy, we just need to remove the lock manually. But to do so, we need <a class="zem_slink" href="http://en.wikipedia.org/wiki/Superuser" title="Superuser" rel="wikipedia" target="_blank">root privileges</a>.<br/>Just with the command:<br/><br/>[code lang="bash"] sudo rm/var/lib/apt/lists/lock [/code] or<br/><br/>[code lang="bash"] sudo rm/var/cache/apt/archives/lock [/code]<br/><br/>Then try to run apt-get command again.<br/>well, that's it. easy isn't it?<br/><br/>I hope this helps anyone.gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com0tag:blogger.com,1999:blog-7546034325993461816.post-79102062808156678402016-04-12T22:10:00.000-07:002016-06-01T17:42:30.576-07:00How to add PPA repository in ubuntu linuxThe PPA is a linux repository on a non-official, used to accommodate the developer's applications which are not included into the indi repository official/official. Often when we want to install an application program on linux, it turns out there hasn't been in the official repository. The existence of this PPA repository, we do not need to download the source application, and then mengcompile himself of course troublesome and not necessarily the application runs perfect.<br/><br/>We just need to add an entry into our repository ppa, and applications that exist on the ppa repository can we install with package manager.<br/><br/>There are several ways to add the ppa repository, I am just going to cover 2 ways, namely through the terminal and gui.<br/>Add the PPA repository through the terminal<br/><br/>Open a terminal emulator, and then enter the command:<br/><br/>[code lang="bash"] sudo add-apt-repository ppa: achadwick/mypaint-testing [/code]<br/><br/>Which the ppa: achadwick/mypaint-testing is the ppa entry that you can get on the website launcpad.net for the application in question.<br/>Add the PPA repository through GUI (Software Source)<br/><br/>Open Software Sources, and then enter your password.<br/><br/>then:<br/><br/>Click the tab of the PPA<br/>Click Add new PPA<br/>Copas entry PPA<br/>Click OK, and then follow the process.<br/>If the process is completed, click refresh list<br/><br/> <br/><img src="http://asudahlah.com/wp-content/uploads/2015/12/Screenshot-from-2015-12-31-140132.jpg" alt="add ubuntu ppa"/>gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com0tag:blogger.com,1999:blog-7546034325993461816.post-42629616427721550382016-04-12T21:50:00.000-07:002016-06-01T17:42:30.567-07:00How to fix missing suspend function in cinnamon ubuntuAfter a long time using xfce in my <a class="zem_slink" href="http://www.ubuntu.com" title="Ubuntu (operating system)" rel="homepage" target="_blank">ubuntu</a> laptop. Finally a few days ago I had to replace my desktop into cinnamon.<br/><br/>Till now, I never turn off/shut down my laptop. Whenever I finished using my laptop, i always suspend it, because i don't want to wait for a long boot time when i want to use it again.<br/><br/>Well, there's a problem showed up after I install cinnamon desktop. There are no suspend menu when click quit in the launcher. Which only shows "Restart", "Cancel" and "Shut Down".<br/><br/>In my case, the source of the problem is the cinnamon default session setting does not use logind. You can use the command to see what the settings are:<br/><br/><code>gsettings get org.cinnamon.desktop.session settings-daemon-uses-logind<br/><br/>gsettings get org. cinnamon. desktop. session session-manager-uses-logind</code><br/><br/>If both of the above commands output "false". Then it means cinnamon session does not use logind. We should set it up manually with these commands:<br/><code>gsettings set org.cinnamon.desktop.session settings-daemon-uses-logind true<br/>gsettings set org.cinnamon.desktop.session session-manager-uses-logind true<br/>gsettings set org.cinnamon.desktop.session screensaver-uses-logind false</code><br/><br/>After restart, suspend button now appears in window session<br/><br/>Finally I can suspend my laptop.<br/><br/>But.... here comes another problem, When the laptop lid is closed, my ubuntu doesn't suspend. Although in the power settings I have set it up that way.<br/><br/>And again, Finally I found a solution, it's very simple, just uncomment a single line in a configuration file:<br/><br/><code>/etc/systemd/logind.conf</code><br/><br/>use your favorite <a class="zem_slink" href="http://en.wikipedia.org/wiki/Text_editor" title="Text editor" rel="wikipedia" target="_blank">text editor</a> to change this, do not forget the sudo command.<br/><br/><code>sudo nano /etc/systemd/logind.conf<br/></code><br/>Locate the line that contains the configuration of "HandleLidSwitch = suspend", then uncheck the fence at the beginning of the line. (uncomment).<br/><br/>Then restart, my "suspend" problems on my ubuntu laptop cinnamon is now gone.<br/><br/>After that, there are many more problems that I encountered during the transition to the cinnamon. which I'll share in another post.<br/><br/>I hope this badly organized post can be useful.gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com0tag:blogger.com,1999:blog-7546034325993461816.post-46290879768760389232016-04-12T18:38:00.000-07:002016-06-01T17:42:30.552-07:00Tutorial membuat Kalkulator IP Address dengan javascript & html (video)Step 1<br/><br/>[code lang="html"]<!DOCTYPE html><br/><html><br/> <head><br/> </head><br/> <body><br/> </body><br/></html>[/code]<br/><br/> <br/><br/>Step 2<br/><br/>[code lang="html"] <title>IP Calculator</title>[/code]<br/><br/>Step 3<br/><br/>[code lang="html"] <h1>IP Address calculator</h1>[/code]<br/><br/>Step 4<br/><br/>[code lang="html"] IP Address<br/> <input type='text' class='addr' id='q1'> .<br/> <input type='text' class='addr' id='q2'> .<br/> <input type='text' class='addr' id='q3'> .<br/> <input type='text' class='addr' id='q4'> /<br/> <input type='text' class='addr' id='cidr'><br/> <button>Calculate</button>[/code]<br/><br/> <br/><br/>Step 5<br/><br/>[code lang="html"] <style><br/> body {<br/> font-family:monospace;<br/> font-size:16px;<br/> }<br/> .addr {<br/> width:30px;<br/> }<br/> </style>[/code]<br/><br/> <br/><br/>Step 6<br/><br/>[code lang="html"] <hr><br/> <div class='result'><br/> <span class=label>IP Address :</span><br/> <span class=value id=resIP></span><br/> </div><br/> <div class='result'><br/> <span class=label>Subnet mask :</span><br/> <span class=value id=resMask></span><br/> </div><br/> <div class='result'><br/> <span class=label>Net Address :</span><br/> <span class=value id=resNet></span><br/> </div><br/> <div class='result'><br/> <span class=label>Broadcast Address :</span><br/> <span class=value id=resBC></span><br/> </div><br/> <div class='result'><br/> <span class=label>Standard Class :</span><br/> <span class=value id=resClass></span><br/> </div><br/> <div class='result'><br/> <span class=label>Range :</span><br/> <span class=value id=resRange></span><br/> </div><br/> <div class='result'><br/> <span class=label>IP Binary :</span><br/> <span class=value id=resBinIP></span><br/> </div><br/> <div class='result'><br/> <span class=label>Mask Binary :</span><br/> <span class=value id=resBinMask></span><br/> </div><br/> <div class='result'><br/> <span class=label>Net Address Binary :</span><br/> <span class=value id=resBinNet></span><br/> </div><br/> <div class='result'><br/> <span class=label>BC Address Binary :</span><br/> <span class=value id=resBinBC></span><br/> </div>[/code]<br/><br/> <br/><br/>Step 7<br/><br/>[code lang="css"].result {<br/> border-bottom: 1px solid #6a6ade;<br/> border-right: 1px solid #6a6ade;<br/>}<br/>.result .label {<br/> display:inline-block;<br/> width:200px;<br/> background:#aaf;<br/>}[/code]<br/><br/>Step 8<br/><br/>[code lang="html"] <script type='text/javascript'><br/> function calculate(){<br/> alert("calculate button pressed");<br/> }<br/> </script>[/code]<br/><br/>Step 9<br/><br/>[code lang="html"] <button onclick='calculate();'>Calculate</button>[/code]<br/><br/>Step 10<br/><br/>[code lang="javascript"]//get values from input box<br/>var q1=document.getElementById('q1').value;<br/>var q2=document.getElementById('q2').value;<br/>var q3=document.getElementById('q3').value;<br/>var q4=document.getElementById('q4').value;<br/>var cidr=document.getElementById('cidr').value;<br/><br/>//validate input value<br/>if(<br/>(q1>=0 && q1<=255) &&<br/>(q2>=0 && q2<=255) &&<br/>(q3>=0 && q3<=255) &&<br/>(q4>=0 && q4<=255) &&<br/>(cidr>=0 && cidr<=32)<br/>){<br/>//display IP address<br/>document.getElementById('resIP').innerHTML=q1 + "." + q2 + "." + q3 + "." + q4;<br/><br/>//get IP Address binaries<br/>var ipBin={};<br/>ipBin[1]=String("00000000"+parseInt(q1,10).toString(2)).slice(-8);<br/>ipBin[2]=String("00000000"+parseInt(q2,10).toString(2)).slice(-8);<br/>ipBin[3]=String("00000000"+parseInt(q3,10).toString(2)).slice(-8);<br/>ipBin[4]=String("00000000"+parseInt(q4,10).toString(2)).slice(-8);<br/><br/>//decide standart class<br/>var standartClass="";<br/>if(q1<=126) {<br/> standartClass="A";<br/>}else if (q1==127) {<br/> standartClass="loopback IP"<br/>}else if (q1>=128 && q1<=191) {<br/> standartClass="B";<br/>}else if (q1>=192 && q1<=223) {<br/> standartClass="C";<br/>}else if (q1>=224 && q1<=239) {<br/> standartClass="D (Multicast Address)";<br/>}else if (q1>=240 && q1<=225) {<br/> standartClass="E (Experimental)";<br/>}else {<br/> standartClass="Out of range";<br/>}<br/><br/>//netmask<br/>var mask=cidr;<br/>var importantBlock=Math.ceil(mask/8);<br/>var importantBlockBinary=ipBin[importantBlock];<br/>var maskBinaryBlockCount=mask%8;<br/>if(maskBinaryBlockCount==0)importantBlock++;<br/>var maskBinaryBlock="";<br/>var maskBlock="";<br/>for(var i=1;i<=8;i++){<br/> if(maskBinaryBlockCount>=i){<br/> maskBinaryBlock+="1";<br/> }else{<br/> maskBinaryBlock+="0";<br/> }<br/>}<br/>//convert binary mask block to decimal<br/>maskBlock=parseInt(maskBinaryBlock,2);<br/><br/>//net & broadcast addr<br/>var netBlockBinary="";<br/>var bcBlockBinary="";<br/>for(var i=1;i<=8;i++){<br/> if(maskBinaryBlock.substr(i-1,1)=="1"){<br/> netBlockBinary+=importantBlockBinary.substr(i-1,1);<br/> bcBlockBinary+=importantBlockBinary.substr(i-1,1);<br/> }else{<br/> netBlockBinary+="0";<br/> bcBlockBinary+="1";<br/> }<br/>}<br/><br/>//put everything together, create a string container variables<br/>var mask="";<br/>var maskBinary="";<br/>var net="";<br/>var bc="";<br/>var netBinary="";<br/>var bcBinary="";<br/>var rangeA="";<br/>var rangeB="";<br/>//loop to put whole strings block together<br/>for(var i=1;i<=4;i++){<br/> if(importantBlock>i) {<br/> //blocks before the important block.<br/> mask+="255";<br/> maskBinary+="11111111";<br/> netBinary+=ipBin[i];<br/> bcBinary+=ipBin[i];<br/> net+=parseInt(ipBin[i],2);<br/> bc+=parseInt(ipBin[i],2);<br/> rangeA+=parseInt(ipBin[i],2);<br/> rangeB+=parseInt(ipBin[i],2);<br/> }else if (importantBlock==i) {<br/> //the important block.<br/> mask+=maskBlock;<br/> maskBinary+=maskBinaryBlock;<br/> netBinary+=netBlockBinary;<br/> bcBinary+=bcBlockBinary;<br/> net+=parseInt(netBlockBinary,2);<br/> bc+=parseInt(bcBlockBinary,2);<br/> rangeA+=(parseInt(netBlockBinary,2)+1);<br/> rangeB+=(parseInt(bcBlockBinary,2)-1);<br/> }else {<br/> //block after the important block.<br/> mask+=0;<br/> maskBinary+="00000000";<br/> netBinary+="00000000";<br/> bcBinary+="11111111";<br/> net+="0";<br/> bc+="255";<br/> rangeA+=0;<br/> rangeB+=255;<br/> }<br/> //add . separator except the last block<br/> if(i<4){<br/> mask+=".";<br/> maskBinary+=".";<br/> netBinary+=".";<br/> bcBinary+=".";<br/> net+=".";<br/> bc+=".";<br/> rangeA+=".";<br/> rangeB+=".";<br/> }<br/>}<br/>//write the results to the page.<br/>document.getElementById('resMask').innerHTML=mask;<br/>document.getElementById('resNet').innerHTML=net;<br/>document.getElementById('resBC').innerHTML=bc;<br/>document.getElementById('resRange').innerHTML=rangeA + " - " + rangeB;<br/>document.getElementById('resBinIP').innerHTML=ipBin[1]+"."+ipBin[2]+"."+ipBin[3]+"."+ipBin[4];<br/>document.getElementById('resBinMask').innerHTML=maskBinary;<br/>document.getElementById('resBinNet').innerHTML=netBinary;<br/>document.getElementById('resBinBC').innerHTML=bcBinary;<br/>document.getElementById('resClass').innerHTML=standartClass;<br/>}else{<br/>alert("invalid value");<br/>}<br/>[/code]<br/><br/><h2>Instruction video</h2><br/><iframe src="https://www.youtube.com/embed/xHQ8TrJOcOA" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen"></iframe><br/><h2>Another version.</h2><br/>As requested by one of my subscriber. I made some customization which allows the calculator to guess subnet id using provided possible hosts number.<br/><br/>[code lang="html"]<!DOCTYPE html><br/><html><br/> <head><br/> <title>IP Calculator</title><br/> <style><br/> body {<br/> font-family:monospace;<br/> font-size:16px;<br/> }<br/> .addr {<br/> width:30px;<br/> }<br/> .result {<br/> border-bottom: 1px solid #6a6ade;<br/> border-right: 1px solid #6a6ade;<br/> }<br/> .result .label {<br/> display:inline-block;<br/> width:200px;<br/> background:#aaf;<br/> }<br/> </style><br/> </head><br/> <body><br/> <h1>IP Address calculator</h1><br/> IP Address<br/> <input type='text' class='addr' id='q1'> .<br/> <input type='text' class='addr' id='q2'> .<br/> <input type='text' class='addr' id='q3'> .<br/> <input type='text' class='addr' id='q4'><br/> <br><br/> # of host <br/> <input type='text' class='addr' id='hostNum'><br><br/> # of subnet <br/> <input type='text' class='addr' id='subnetNum'><br/> <br><br/> Note:<br/> <br><br/> Netmask will be automatically guessed using #of host first<br/> <br><br/> # of subnet isn't really used here as the netmask is already guessed from # of host<br/> <br><br/> <button onclick='calculate();'>Calculate</button><br/> <hr><br/> <div class='result'><br/> <span class=label>IP Address :</span><br/> <span class=value id=resIP></span><br/> </div><br/> <div class='result'><br/> <span class=label>Subnet mask :</span><br/> <span class=value id=resMask></span><br/> </div><br/> <div class='result'><br/> <span class=label>Subnet Id (CIDR) :</span><br/> <span class=value id=resSubnetId></span><br/> </div><br/> <div class='result'><br/> <span class=label>Net Address :</span><br/> <span class=value id=resNet></span><br/> </div><br/> <div class='result'><br/> <span class=label>Broadcast Address :</span><br/> <span class=value id=resBC></span><br/> </div><br/> <div class='result'><br/> <span class=label>Standard Class :</span><br/> <span class=value id=resClass></span><br/> </div><br/> <div class='result'><br/> <span class=label>Important Block :</span><br/> <span class=value id=resImportantBlock></span><br/> </div><br/> <div class='result'><br/> <span class=label>Range :</span><br/> <span class=value id=resRange></span><br/> </div><br/> <div class='result'><br/> <span class=label>IP Binary :</span><br/> <span class=value id=resBinIP></span><br/> </div><br/> <div class='result'><br/> <span class=label>Mask Binary :</span><br/> <span class=value id=resBinMask></span><br/> </div><br/> <div class='result'><br/> <span class=label>Net Address Binary :</span><br/> <span class=value id=resBinNet></span><br/> </div><br/> <div class='result'><br/> <span class=label>BC Address Binary :</span><br/> <span class=value id=resBinBC></span><br/> </div><br/> <div class='result'><br/> <span class=label>Max # of Subnet :</span><br/> <span class=value id=resMaxNet></span><br/> </div><br/> <div class='result'><br/> <span class=label>Max # of Host :</span><br/> <span class=value id=resMaxHost></span><br/> </div><br/> <div style=font-size:11px;><a href="http://asudahlah.com">By. Gemul Cybermujahidz</a></div><br/> </body><br/> <script type='text/javascript'><br/> function calculate(){<br/> //get values from input box<br/> var q1=document.getElementById('q1').value;<br/> var q2=document.getElementById('q2').value;<br/> var q3=document.getElementById('q3').value;<br/> var q4=document.getElementById('q4').value;<br/> //var cidr=document.getElementById('cidr').value;<br/> var netNum=document.getElementById('subnetNum').value;<br/> var hostNum=document.getElementById('hostNum').value;<br/><br/> //guessing netmask by # of host<br/> var hostNumDbg=0;<br/> for(var i=32;i>=0;i--){<br/> if(hostNum >= Math.pow(2,i)){<br/> //hostNumDbg=Math.pow(2,i+1);<br/> hostNumDbg=32-(i+1);<br/> break;<br/> }<br/> }<br/> var cidr=hostNumDbg;<br/><br/> <br/> //validate input value<br/> if(<br/> (q1>=0 && q1<=255) &&<br/> (q2>=0 && q2<=255) &&<br/> (q3>=0 && q3<=255) &&<br/> (q4>=0 && q4<=255) &&<br/> (cidr>=0 && cidr<=32)<br/> ){<br/> //display IP address<br/> document.getElementById('resIP').innerHTML=q1 + "." + q2 + "." + q3 + "." + q4;<br/><br/> //get IP Address binaries<br/> var ipBin={};<br/> ipBin[1]=String("00000000"+parseInt(q1,10).toString(2)).slice(-8);<br/> ipBin[2]=String("00000000"+parseInt(q2,10).toString(2)).slice(-8);<br/> ipBin[3]=String("00000000"+parseInt(q3,10).toString(2)).slice(-8);<br/> ipBin[4]=String("00000000"+parseInt(q4,10).toString(2)).slice(-8);<br/><br/> //decide standart class<br/> <br/> var standartClass="";<br/> if(q1<=126) {<br/> standartClass="A";<br/> }else if (q1==127) {<br/> standartClass="loopback IP"<br/> }else if (q1>=128 && q1<=191) {<br/> standartClass="B";<br/> }else if (q1>=192 && q1<=223) {<br/> standartClass="C";<br/> }else if (q1>=224 && q1<=239) {<br/> standartClass="D (Multicast Address)";<br/> }else if (q1>=240 && q1<=225) {<br/> standartClass="E (Experimental)";<br/> }else {<br/> standartClass="Out of range";<br/> }<br/> <br/> //netmask<br/> var mask=cidr;<br/> var importantBlock=Math.ceil(mask/8);<br/> var importantBlockBinary=ipBin[importantBlock];<br/> var maskBinaryBlockCount=mask%8;<br/> if(maskBinaryBlockCount==0)importantBlock++;<br/> var maskBinaryBlock="";<br/> var maskBlock="";<br/> for(var i=1;i<=8;i++){<br/> if(maskBinaryBlockCount>=i){<br/> maskBinaryBlock+="1";<br/> }else{<br/> maskBinaryBlock+="0";<br/> }<br/> }<br/> //convert binary mask block to decimal<br/> maskBlock=parseInt(maskBinaryBlock,2);<br/><br/> //net & broadcast addr<br/> var netBlockBinary="";<br/> var bcBlockBinary="";<br/> for(var i=1;i<=8;i++){<br/> if(maskBinaryBlock.substr(i-1,1)=="1"){<br/> netBlockBinary+=importantBlockBinary.substr(i-1,1);<br/> bcBlockBinary+=importantBlockBinary.substr(i-1,1);<br/> }else{<br/> netBlockBinary+="0";<br/> bcBlockBinary+="1";<br/> }<br/> }<br/><br/> //put everything together, create a string container variables<br/> var mask="";<br/> var maskBinary="";<br/> var net="";<br/> var bc="";<br/> var netBinary="";<br/> var bcBinary="";<br/> var rangeA="";<br/> var rangeB="";<br/> //loop to put whole strings block together<br/> for(var i=1;i<=4;i++){<br/> if(importantBlock>i) {<br/> //blocks before the important block.<br/> mask+="255";<br/> maskBinary+="11111111";<br/> netBinary+=ipBin[i];<br/> bcBinary+=ipBin[i];<br/> net+=parseInt(ipBin[i],2);<br/> bc+=parseInt(ipBin[i],2);<br/> rangeA+=parseInt(ipBin[i],2);<br/> rangeB+=parseInt(ipBin[i],2);<br/> }else if (importantBlock==i) {<br/> //the important block.<br/> mask+=maskBlock;<br/> maskBinary+=maskBinaryBlock;<br/> netBinary+=netBlockBinary;<br/> bcBinary+=bcBlockBinary;<br/> net+=parseInt(netBlockBinary,2);<br/> bc+=parseInt(bcBlockBinary,2);<br/> rangeA+=(parseInt(netBlockBinary,2)+1);<br/> rangeB+=(parseInt(bcBlockBinary,2)-1);<br/> }else {<br/> //block after the important block.<br/> mask+=0;<br/> maskBinary+="00000000";<br/> netBinary+="00000000";<br/> bcBinary+="11111111";<br/> net+="0";<br/> bc+="255";<br/> rangeA+=0;<br/> rangeB+=255;<br/> }<br/> //add . separator except the last block<br/> if(i<4){<br/> mask+=".";<br/> maskBinary+=".";<br/> netBinary+=".";<br/> bcBinary+=".";<br/> net+=".";<br/> bc+=".";<br/> rangeA+=".";<br/> rangeB+=".";<br/> }<br/> }<br/> <br/> //additional : count maximum host, maximum net and current subnets<br/> var binaryHost="";<br/> for(var i=(31-cidr);i>=0;i--){<br/> binaryHost=binaryHost+"1";<br/> }<br/> var maxHost=parseInt(binaryHost,2);<br/> var binarySubnet="";<br/> for(var i=cidr;i>=0;i--){<br/> binarySubnet=binarySubnet+"1";<br/> }<br/> var maxSubnet=parseInt(binarySubnet,2);<br/> var binaryCurrentSubnetBlock="";<br/> for(var i=maskBinaryBlockCount;i>=0;i--){<br/> binaryCurrentSubnetBlock=binaryCurrentSubnetBlock+"1";<br/> }<br/> var maxCurrentSubnetBlock=parseInt(binaryCurrentSubnetBlock,2);<br/> <br/> //write the results to the page.<br/> document.getElementById('resMask').innerHTML=mask;<br/> document.getElementById('resNet').innerHTML=net;<br/> document.getElementById('resBC').innerHTML=bc;<br/> document.getElementById('resRange').innerHTML=rangeA + " - " + rangeB;<br/> document.getElementById('resBinIP').innerHTML=ipBin[1]+"."+ipBin[2]+"."+ipBin[3]+"."+ipBin[4];<br/> document.getElementById('resBinMask').innerHTML=maskBinary;<br/> document.getElementById('resBinNet').innerHTML=netBinary;<br/> document.getElementById('resBinBC').innerHTML=bcBinary;<br/> document.getElementById('resClass').innerHTML=standartClass;<br/> document.getElementById('resSubnetId').innerHTML=cidr;<br/> document.getElementById('resMaxHost').innerHTML=maxHost+" possible host(s) in current subnet";<br/> document.getElementById('resMaxNet').innerHTML=maxSubnet+" of total possible subnet, "+maxCurrentSubnetBlock+" possible subnet in current block";<br/> document.getElementById('resImportantBlock').innerHTML=importantBlock;<br/> }else{<br/> alert("invalid value");<br/> }<br/><br/> }<br/> </script><br/></html><br/>[/code]gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com5tag:blogger.com,1999:blog-7546034325993461816.post-91163415171826059112016-04-12T18:22:00.000-07:002018-06-25T00:59:31.558-07:00TP-Link Access Point/router batch reboot/restarter php scriptUntuk versi bahasa indonesia, silahkan klik <a href="http://asudahlah.com/2015/12/tp-link-access-pointrouter-batch-rebootrestarter-php-script-id/">disini</a><br />
<br />
If you have a large wireless network infrastructure, which consist of many Access point and Wi-Fi router. And at some cases, you need to safely reboot all access point. It could be very annoying if you have to login to each AP/Router and restart them individually one by one.<br />
<br />
Why bother doing all those work manually when you could write a code to do it automatically?<br />
<br />
Well, luckily in my cases, all of the access point have the same brand. it's TP-Link. After some research, i found a way to reboot those brand's access point and router by using URL. Then i wrote this PHP script to get the job done.<br />
<br />
In my infrastructure, i made all access point and router have different IP sub from DHCP-ed network. All have same username and password, all have the same method of authentication (using http auth). This script written in PHP, running in terminal/console. So it's very handy when it comes to automation. Just add it to cron and voila. Don't forget to make it executable "chmod 777" if you want to run it under linux and make sure you have php5-cli installed.<br />
<br />
Here i share the script i wrote, you can customize it to suit your infrastructure, be creative.<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #888888;">#!/usr/bin/php</span>
<?php
//----------Auth config
<span style="color: #996633;">$apUser</span><span style="color: #333333;">=</span><span style="background-color: #fff0f0;">"admin"</span>;
<span style="color: #996633;">$apPass</span><span style="color: #333333;">=</span><span style="background-color: #fff0f0;">"admin"</span>;
//----------Lists of AP and it<span style="background-color: #fff0f0;">'s IP address</span>
<span style="background-color: #fff0f0;">$apList=Array(</span>
<span style="background-color: #fff0f0;">Array("Ext1","192.168.0.15"),</span>
<span style="background-color: #fff0f0;">Array("Ext2","192.168.0.17"),</span>
<span style="background-color: #fff0f0;">Array("Jalak","192.168.0.2"),</span>
<span style="background-color: #fff0f0;">Array("Barat","192.168.0.3"),</span>
<span style="background-color: #fff0f0;">Array("-","192.168.0.5"),</span>
<span style="background-color: #fff0f0;">Array("Parkit","192.168.0.6"),</span>
<span style="background-color: #fff0f0;">Array("Murai","192.168.0.9"),</span>
<span style="background-color: #fff0f0;">Array("Nuri","192.168.0.10"),</span>
<span style="background-color: #fff0f0;">Array("Kenari","192.168.0.12"),</span>
<span style="background-color: #fff0f0;">Array("Cucak","192.168.0.13"),</span>
<span style="background-color: #fff0f0;">Array("Hud-Hud","192.168.0.14"),</span>
<span style="background-color: #fff0f0;">);</span>
<span style="background-color: #fff0f0;">function curl_download($ip){</span>
<span style="background-color: #fff0f0;">$Url="http://".$ip."/userRpm/SysRebootRpm.htm?Reboot=Reboot";</span>
<span style="background-color: #fff0f0;">// is cURL installed yet?</span>
<span style="background-color: #fff0f0;">if (!function_exists('</span>curl_init<span style="background-color: #fff0f0;">')){</span>
<span style="background-color: #fff0f0;">die('</span>Sorry cURL is not installed!<span style="background-color: #fff0f0;">');</span>
<span style="background-color: #fff0f0;">}</span>
<span style="background-color: #fff0f0;">// OK cool - then let'</span>s create a new cURL resource handle
<span style="color: #996633;">$ch</span> <span style="color: #333333;">=</span> curl_init<span style="color: #333333;">()</span>;
// Set URL to download
curl_setopt<span style="color: #333333;">(</span><span style="color: #996633;">$ch</span>, CURLOPT_REFERER, <span style="background-color: #fff0f0;">"http://"</span>.<span style="color: #996633;">$ip</span>.<span style="background-color: #fff0f0;">"/userRpm/SysRebootRpm.htm"</span><span style="color: #333333;">)</span>;
curl_setopt<span style="color: #333333;">(</span><span style="color: #996633;">$ch</span>, CURLOPT_USERAGENT, <span style="background-color: #fff0f0;">"Mozilla/5.0 (X11; Ubuntu; Linux i686;rv:12.0) Gecko/20100101 Firefox/12.0"</span><span style="color: #333333;">)</span>;
curl_setopt<span style="color: #333333;">(</span><span style="color: #996633;">$ch</span>, CURLOPT_RETURNTRANSFER, <span style="color: #007020;">true</span><span style="color: #333333;">)</span>;
curl_setopt<span style="color: #333333;">(</span><span style="color: #996633;">$ch</span>, CURLOPT_URL, <span style="color: #996633;">$Url</span><span style="color: #333333;">)</span>;
curl_setopt<span style="color: #333333;">(</span><span style="color: #996633;">$ch</span>, CURLOPT_HTTPAUTH, CURLAUTH_ANY<span style="color: #333333;">)</span>;
curl_setopt<span style="color: #333333;">(</span><span style="color: #996633;">$ch</span>, CURLOPT_USERPWD, <span style="color: #996633;">$apUser</span>.<span style="background-color: #fff0f0;">":"</span>.<span style="color: #996633;">$apPass</span><span style="color: #333333;">)</span>;
curl_setopt<span style="color: #333333;">(</span><span style="color: #996633;">$ch</span>, CURLOPT_TIMEOUT, 120<span style="color: #333333;">)</span>;
<span style="color: #996633;">$output</span> <span style="color: #333333;">=</span> curl_exec<span style="color: #333333;">(</span><span style="color: #996633;">$ch</span><span style="color: #333333;">)</span>;
curl_close<span style="color: #333333;">(</span><span style="color: #996633;">$ch</span><span style="color: #333333;">)</span>;
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #996633;">$output</span>;
<span style="color: #333333;">}</span>
print <span style="background-color: #fff0f0;">"\n"</span>;
print <span style="background-color: #fff0f0;">"\e[1;34mAP Resetter. By Cybermujahidz\e[0m\n"</span>;
print <span style="background-color: #fff0f0;">"\n"</span>;
print <span style="background-color: #fff0f0;">"List of AP:\n"</span>;
foreach<span style="color: #333333;">(</span><span style="color: #996633;">$apList</span> as <span style="color: #996633;">$ap</span><span style="color: #333333;">){</span>
print str_pad<span style="color: #333333;">(</span><span style="color: #996633;">$ap</span><span style="color: #333333;">[</span>1<span style="color: #333333;">]</span>,15,<span style="background-color: #fff0f0;">' '</span>,STR_PAD_RIGHT<span style="color: #333333;">)</span>.<span style="background-color: #fff0f0;">" "</span>.str_pad<span style="color: #333333;">(</span><span style="color: #996633;">$ap</span><span style="color: #333333;">[</span>0<span style="color: #333333;">]</span>,10,<span style="background-color: #fff0f0;">' '</span>,STR_PAD_RIGHT<span style="color: #333333;">)</span>.<span style="background-color: #fff0f0;">" queued\n"</span>;
<span style="color: #333333;">}</span>
print <span style="background-color: #fff0f0;">"\n"</span>;
print <span style="background-color: #fff0f0;">"Resetting ALL AP\n"</span>;
print <span style="background-color: #fff0f0;">"----------------\n"</span>;
foreach<span style="color: #333333;">(</span><span style="color: #996633;">$apList</span> as <span style="color: #996633;">$ap</span><span style="color: #333333;">){</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>curl_download<span style="color: #333333;">(</span><span style="color: #996633;">$ap</span><span style="color: #333333;">[</span>1<span style="color: #333333;">])){</span>
print str_pad<span style="color: #333333;">(</span><span style="color: #996633;">$ap</span><span style="color: #333333;">[</span>1<span style="color: #333333;">]</span>,15,<span style="background-color: #fff0f0;">' '</span>,STR_PAD_RIGHT<span style="color: #333333;">)</span>.<span style="background-color: #fff0f0;">" "</span>.str_pad<span style="color: #333333;">(</span><span style="color: #996633;">$ap</span><span style="color: #333333;">[</span>0<span style="color: #333333;">]</span>,10,<span style="background-color: #fff0f0;">' '</span>,STR_PAD_RIGHT<span style="color: #333333;">)</span>.<span style="background-color: #fff0f0;">" [ \e[1;32mAP Rebooted\e[0m ]\n"</span>;
<span style="color: #333333;">}</span><span style="color: #008800; font-weight: bold;">else</span><span style="color: #333333;">{</span>
print str_pad<span style="color: #333333;">(</span><span style="color: #996633;">$ap</span><span style="color: #333333;">[</span>1<span style="color: #333333;">]</span>,15,<span style="background-color: #fff0f0;">' '</span>,STR_PAD_RIGHT<span style="color: #333333;">)</span>.<span style="background-color: #fff0f0;">" "</span>.str_pad<span style="color: #333333;">(</span><span style="color: #996633;">$ap</span><span style="color: #333333;">[</span>0<span style="color: #333333;">]</span>,10,<span style="background-color: #fff0f0;">' '</span>,STR_PAD_RIGHT<span style="color: #333333;">)</span>.<span style="background-color: #fff0f0;">" [ \e[1;31m AP Down \e[0m ]\n"</span>;
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
print <span style="background-color: #fff0f0;">"AP reset finished.\n"</span>;
print <span style="background-color: #fff0f0;">"------------------\n"</span>;
?>
</pre>
</div>
<br />gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com140tag:blogger.com,1999:blog-7546034325993461816.post-79468674467933635712016-01-24T03:53:00.000-08:002016-06-01T17:42:30.523-07:00How to make IP Address Calculator using HTML & JavascriptStep 1<br/><br/>[code lang="html"]<!DOCTYPE html><br/><html><br/> <head><br/> </head><br/> <body><br/> </body><br/></html>[/code]<br/><br/> <br/><br/>Step 2<br/><br/>[code lang="html"] <title>IP Calculator</title>[/code]<br/><br/>Step 3<br/><br/>[code lang="html"] <h1>IP Address calculator</h1>[/code]<br/><br/>Step 4<br/><br/>[code lang="html"] IP Address<br/> <input type='text' class='addr' id='q1'> .<br/> <input type='text' class='addr' id='q2'> .<br/> <input type='text' class='addr' id='q3'> .<br/> <input type='text' class='addr' id='q4'> /<br/> <input type='text' class='addr' id='cidr'><br/> <button>Calculate</button>[/code]<br/><br/> <br/><br/>Step 5<br/><br/>[code lang="html"] <style><br/> body {<br/> font-family:monospace;<br/> font-size:16px;<br/> }<br/> .addr {<br/> width:30px;<br/> }<br/> </style>[/code]<br/><br/> <br/><br/>Step 6<br/><br/>[code lang="html"] <hr><br/> <div class='result'><br/> <span class=label>IP Address :</span><br/> <span class=value id=resIP></span><br/> </div><br/> <div class='result'><br/> <span class=label>Subnet mask :</span><br/> <span class=value id=resMask></span><br/> </div><br/> <div class='result'><br/> <span class=label>Net Address :</span><br/> <span class=value id=resNet></span><br/> </div><br/> <div class='result'><br/> <span class=label>Broadcast Address :</span><br/> <span class=value id=resBC></span><br/> </div><br/> <div class='result'><br/> <span class=label>Standard Class :</span><br/> <span class=value id=resClass></span><br/> </div><br/> <div class='result'><br/> <span class=label>Range :</span><br/> <span class=value id=resRange></span><br/> </div><br/> <div class='result'><br/> <span class=label>IP Binary :</span><br/> <span class=value id=resBinIP></span><br/> </div><br/> <div class='result'><br/> <span class=label>Mask Binary :</span><br/> <span class=value id=resBinMask></span><br/> </div><br/> <div class='result'><br/> <span class=label>Net Address Binary :</span><br/> <span class=value id=resBinNet></span><br/> </div><br/> <div class='result'><br/> <span class=label>BC Address Binary :</span><br/> <span class=value id=resBinBC></span><br/> </div>[/code]<br/><br/> <br/><br/>Step 7<br/><br/>[code lang="css"].result {<br/> border-bottom: 1px solid #6a6ade;<br/> border-right: 1px solid #6a6ade;<br/>}<br/>.result .label {<br/> display:inline-block;<br/> width:200px;<br/> background:#aaf;<br/>}[/code]<br/><br/>Step 8<br/><br/>[code lang="html"] <script type='text/javascript'><br/> function calculate(){<br/> alert("calculate button pressed");<br/> }<br/> </script>[/code]<br/><br/>Step 9<br/><br/>[code lang="html"] <button onclick='calculate();'>Calculate</button>[/code]<br/><br/>Step 10<br/><br/>[code lang="javascript"]//get values from input box<br/>var q1=document.getElementById('q1').value;<br/>var q2=document.getElementById('q2').value;<br/>var q3=document.getElementById('q3').value;<br/>var q4=document.getElementById('q4').value;<br/>var cidr=document.getElementById('cidr').value;<br/><br/>//validate input value<br/>if(<br/>(q1>=0 && q1<=255) &&<br/>(q2>=0 && q2<=255) &&<br/>(q3>=0 && q3<=255) &&<br/>(q4>=0 && q4<=255) &&<br/>(cidr>=0 && cidr<=32)<br/>){<br/>//display IP address<br/>document.getElementById('resIP').innerHTML=q1 + "." + q2 + "." + q3 + "." + q4;<br/><br/>//get IP Address binaries<br/>var ipBin={};<br/>ipBin[1]=String("00000000"+parseInt(q1,10).toString(2)).slice(-8);<br/>ipBin[2]=String("00000000"+parseInt(q2,10).toString(2)).slice(-8);<br/>ipBin[3]=String("00000000"+parseInt(q3,10).toString(2)).slice(-8);<br/>ipBin[4]=String("00000000"+parseInt(q4,10).toString(2)).slice(-8);<br/><br/>//decide standart class<br/>var standartClass="";<br/>if(q1<=126) {<br/> standartClass="A";<br/>}else if (q1==127) {<br/> standartClass="loopback IP"<br/>}else if (q1>=128 && q1<=191) {<br/> standartClass="B";<br/>}else if (q1>=192 && q1<=223) {<br/> standartClass="C";<br/>}else if (q1>=224 && q1<=239) {<br/> standartClass="D (Multicast Address)";<br/>}else if (q1>=240 && q1<=225) {<br/> standartClass="E (Experimental)";<br/>}else {<br/> standartClass="Out of range";<br/>}<br/><br/>//netmask<br/>var mask=cidr;<br/>var importantBlock=Math.ceil(mask/8);<br/>var importantBlockBinary=ipBin[importantBlock];<br/>var maskBinaryBlockCount=mask%8;<br/>if(maskBinaryBlockCount==0)importantBlock++;<br/>var maskBinaryBlock="";<br/>var maskBlock="";<br/>for(var i=1;i<=8;i++){<br/> if(maskBinaryBlockCount>=i){<br/> maskBinaryBlock+="1";<br/> }else{<br/> maskBinaryBlock+="0";<br/> }<br/>}<br/>//convert binary mask block to decimal<br/>maskBlock=parseInt(maskBinaryBlock,2);<br/><br/>//net & broadcast addr<br/>var netBlockBinary="";<br/>var bcBlockBinary="";<br/>for(var i=1;i<=8;i++){<br/> if(maskBinaryBlock.substr(i-1,1)=="1"){<br/> netBlockBinary+=importantBlockBinary.substr(i-1,1);<br/> bcBlockBinary+=importantBlockBinary.substr(i-1,1);<br/> }else{<br/> netBlockBinary+="0";<br/> bcBlockBinary+="1";<br/> }<br/>}<br/><br/>//put everything together, create a string container variables<br/>var mask="";<br/>var maskBinary="";<br/>var net="";<br/>var bc="";<br/>var netBinary="";<br/>var bcBinary="";<br/>var rangeA="";<br/>var rangeB="";<br/>//loop to put whole strings block together<br/>for(var i=1;i<=4;i++){<br/> if(importantBlock>i) {<br/> //blocks before the important block.<br/> mask+="255";<br/> maskBinary+="11111111";<br/> netBinary+=ipBin[i];<br/> bcBinary+=ipBin[i];<br/> net+=parseInt(ipBin[i],2);<br/> bc+=parseInt(ipBin[i],2);<br/> rangeA+=parseInt(ipBin[i],2);<br/> rangeB+=parseInt(ipBin[i],2);<br/> }else if (importantBlock==i) {<br/> //the important block.<br/> mask+=maskBlock;<br/> maskBinary+=maskBinaryBlock;<br/> netBinary+=netBlockBinary;<br/> bcBinary+=bcBlockBinary;<br/> net+=parseInt(netBlockBinary,2);<br/> bc+=parseInt(bcBlockBinary,2);<br/> rangeA+=(parseInt(netBlockBinary,2)+1);<br/> rangeB+=(parseInt(bcBlockBinary,2)-1);<br/> }else {<br/> //block after the important block.<br/> mask+=0;<br/> maskBinary+="00000000";<br/> netBinary+="00000000";<br/> bcBinary+="11111111";<br/> net+="0";<br/> bc+="255";<br/> rangeA+=0;<br/> rangeB+=255;<br/> }<br/> //add . separator except the last block<br/> if(i<4){<br/> mask+=".";<br/> maskBinary+=".";<br/> netBinary+=".";<br/> bcBinary+=".";<br/> net+=".";<br/> bc+=".";<br/> rangeA+=".";<br/> rangeB+=".";<br/> }<br/>}<br/>//write the results to the page.<br/>document.getElementById('resMask').innerHTML=mask;<br/>document.getElementById('resNet').innerHTML=net;<br/>document.getElementById('resBC').innerHTML=bc;<br/>document.getElementById('resRange').innerHTML=rangeA + " - " + rangeB;<br/>document.getElementById('resBinIP').innerHTML=ipBin[1]+"."+ipBin[2]+"."+ipBin[3]+"."+ipBin[4];<br/>document.getElementById('resBinMask').innerHTML=maskBinary;<br/>document.getElementById('resBinNet').innerHTML=netBinary;<br/>document.getElementById('resBinBC').innerHTML=bcBinary;<br/>document.getElementById('resClass').innerHTML=standartClass;<br/>}else{<br/>alert("invalid value");<br/>}<br/>[/code]<br/><br/><h2>Instruction video</h2><br/><iframe src="https://www.youtube.com/embed/xHQ8TrJOcOA" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen"></iframe><br/><h2>Another version.</h2><br/>As requested by one of my subscriber. I made some customization which allows the calculator to guess subnet id using provided possible hosts number.<br/><br/>[code lang="html"]<!DOCTYPE html><br/><html><br/> <head><br/> <title>IP Calculator</title><br/> <style><br/> body {<br/> font-family:monospace;<br/> font-size:16px;<br/> }<br/> .addr {<br/> width:30px;<br/> }<br/> .result {<br/> border-bottom: 1px solid #6a6ade;<br/> border-right: 1px solid #6a6ade;<br/> }<br/> .result .label {<br/> display:inline-block;<br/> width:200px;<br/> background:#aaf;<br/> }<br/> </style><br/> </head><br/> <body><br/> <h1>IP Address calculator</h1><br/> IP Address<br/> <input type='text' class='addr' id='q1'> .<br/> <input type='text' class='addr' id='q2'> .<br/> <input type='text' class='addr' id='q3'> .<br/> <input type='text' class='addr' id='q4'><br/> <br><br/> # of host <br/> <input type='text' class='addr' id='hostNum'><br><br/> # of subnet <br/> <input type='text' class='addr' id='subnetNum'><br/> <br><br/> Note:<br/> <br><br/> Netmask will be automatically guessed using #of host first<br/> <br><br/> # of subnet isn't really used here as the netmask is already guessed from # of host<br/> <br><br/> <button onclick='calculate();'>Calculate</button><br/> <hr><br/> <div class='result'><br/> <span class=label>IP Address :</span><br/> <span class=value id=resIP></span><br/> </div><br/> <div class='result'><br/> <span class=label>Subnet mask :</span><br/> <span class=value id=resMask></span><br/> </div><br/> <div class='result'><br/> <span class=label>Subnet Id (CIDR) :</span><br/> <span class=value id=resSubnetId></span><br/> </div><br/> <div class='result'><br/> <span class=label>Net Address :</span><br/> <span class=value id=resNet></span><br/> </div><br/> <div class='result'><br/> <span class=label>Broadcast Address :</span><br/> <span class=value id=resBC></span><br/> </div><br/> <div class='result'><br/> <span class=label>Standard Class :</span><br/> <span class=value id=resClass></span><br/> </div><br/> <div class='result'><br/> <span class=label>Important Block :</span><br/> <span class=value id=resImportantBlock></span><br/> </div><br/> <div class='result'><br/> <span class=label>Range :</span><br/> <span class=value id=resRange></span><br/> </div><br/> <div class='result'><br/> <span class=label>IP Binary :</span><br/> <span class=value id=resBinIP></span><br/> </div><br/> <div class='result'><br/> <span class=label>Mask Binary :</span><br/> <span class=value id=resBinMask></span><br/> </div><br/> <div class='result'><br/> <span class=label>Net Address Binary :</span><br/> <span class=value id=resBinNet></span><br/> </div><br/> <div class='result'><br/> <span class=label>BC Address Binary :</span><br/> <span class=value id=resBinBC></span><br/> </div><br/> <div class='result'><br/> <span class=label>Max # of Subnet :</span><br/> <span class=value id=resMaxNet></span><br/> </div><br/> <div class='result'><br/> <span class=label>Max # of Host :</span><br/> <span class=value id=resMaxHost></span><br/> </div><br/> <div style=font-size:11px;><a href="http://asudahlah.com">By. Gemul Cybermujahidz</a></div><br/> </body><br/> <script type='text/javascript'><br/> function calculate(){<br/> //get values from input box<br/> var q1=document.getElementById('q1').value;<br/> var q2=document.getElementById('q2').value;<br/> var q3=document.getElementById('q3').value;<br/> var q4=document.getElementById('q4').value;<br/> //var cidr=document.getElementById('cidr').value;<br/> var netNum=document.getElementById('subnetNum').value;<br/> var hostNum=document.getElementById('hostNum').value;<br/><br/> //guessing netmask by # of host<br/> var hostNumDbg=0;<br/> for(var i=32;i>=0;i--){<br/> if(hostNum >= Math.pow(2,i)){<br/> //hostNumDbg=Math.pow(2,i+1);<br/> hostNumDbg=32-(i+1);<br/> break;<br/> }<br/> }<br/> var cidr=hostNumDbg;<br/><br/> <br/> //validate input value<br/> if(<br/> (q1>=0 && q1<=255) &&<br/> (q2>=0 && q2<=255) &&<br/> (q3>=0 && q3<=255) &&<br/> (q4>=0 && q4<=255) &&<br/> (cidr>=0 && cidr<=32)<br/> ){<br/> //display IP address<br/> document.getElementById('resIP').innerHTML=q1 + "." + q2 + "." + q3 + "." + q4;<br/><br/> //get IP Address binaries<br/> var ipBin={};<br/> ipBin[1]=String("00000000"+parseInt(q1,10).toString(2)).slice(-8);<br/> ipBin[2]=String("00000000"+parseInt(q2,10).toString(2)).slice(-8);<br/> ipBin[3]=String("00000000"+parseInt(q3,10).toString(2)).slice(-8);<br/> ipBin[4]=String("00000000"+parseInt(q4,10).toString(2)).slice(-8);<br/><br/> //decide standart class<br/> <br/> var standartClass="";<br/> if(q1<=126) {<br/> standartClass="A";<br/> }else if (q1==127) {<br/> standartClass="loopback IP"<br/> }else if (q1>=128 && q1<=191) {<br/> standartClass="B";<br/> }else if (q1>=192 && q1<=223) {<br/> standartClass="C";<br/> }else if (q1>=224 && q1<=239) {<br/> standartClass="D (Multicast Address)";<br/> }else if (q1>=240 && q1<=225) {<br/> standartClass="E (Experimental)";<br/> }else {<br/> standartClass="Out of range";<br/> }<br/> <br/> //netmask<br/> var mask=cidr;<br/> var importantBlock=Math.ceil(mask/8);<br/> var importantBlockBinary=ipBin[importantBlock];<br/> var maskBinaryBlockCount=mask%8;<br/> if(maskBinaryBlockCount==0)importantBlock++;<br/> var maskBinaryBlock="";<br/> var maskBlock="";<br/> for(var i=1;i<=8;i++){<br/> if(maskBinaryBlockCount>=i){<br/> maskBinaryBlock+="1";<br/> }else{<br/> maskBinaryBlock+="0";<br/> }<br/> }<br/> //convert binary mask block to decimal<br/> maskBlock=parseInt(maskBinaryBlock,2);<br/><br/> //net & broadcast addr<br/> var netBlockBinary="";<br/> var bcBlockBinary="";<br/> for(var i=1;i<=8;i++){<br/> if(maskBinaryBlock.substr(i-1,1)=="1"){<br/> netBlockBinary+=importantBlockBinary.substr(i-1,1);<br/> bcBlockBinary+=importantBlockBinary.substr(i-1,1);<br/> }else{<br/> netBlockBinary+="0";<br/> bcBlockBinary+="1";<br/> }<br/> }<br/><br/> //put everything together, create a string container variables<br/> var mask="";<br/> var maskBinary="";<br/> var net="";<br/> var bc="";<br/> var netBinary="";<br/> var bcBinary="";<br/> var rangeA="";<br/> var rangeB="";<br/> //loop to put whole strings block together<br/> for(var i=1;i<=4;i++){<br/> if(importantBlock>i) {<br/> //blocks before the important block.<br/> mask+="255";<br/> maskBinary+="11111111";<br/> netBinary+=ipBin[i];<br/> bcBinary+=ipBin[i];<br/> net+=parseInt(ipBin[i],2);<br/> bc+=parseInt(ipBin[i],2);<br/> rangeA+=parseInt(ipBin[i],2);<br/> rangeB+=parseInt(ipBin[i],2);<br/> }else if (importantBlock==i) {<br/> //the important block.<br/> mask+=maskBlock;<br/> maskBinary+=maskBinaryBlock;<br/> netBinary+=netBlockBinary;<br/> bcBinary+=bcBlockBinary;<br/> net+=parseInt(netBlockBinary,2);<br/> bc+=parseInt(bcBlockBinary,2);<br/> rangeA+=(parseInt(netBlockBinary,2)+1);<br/> rangeB+=(parseInt(bcBlockBinary,2)-1);<br/> }else {<br/> //block after the important block.<br/> mask+=0;<br/> maskBinary+="00000000";<br/> netBinary+="00000000";<br/> bcBinary+="11111111";<br/> net+="0";<br/> bc+="255";<br/> rangeA+=0;<br/> rangeB+=255;<br/> }<br/> //add . separator except the last block<br/> if(i<4){<br/> mask+=".";<br/> maskBinary+=".";<br/> netBinary+=".";<br/> bcBinary+=".";<br/> net+=".";<br/> bc+=".";<br/> rangeA+=".";<br/> rangeB+=".";<br/> }<br/> }<br/> <br/> //additional : count maximum host, maximum net and current subnets<br/> var binaryHost="";<br/> for(var i=(31-cidr);i>=0;i--){<br/> binaryHost=binaryHost+"1";<br/> }<br/> var maxHost=parseInt(binaryHost,2);<br/> var binarySubnet="";<br/> for(var i=cidr;i>=0;i--){<br/> binarySubnet=binarySubnet+"1";<br/> }<br/> var maxSubnet=parseInt(binarySubnet,2);<br/> var binaryCurrentSubnetBlock="";<br/> for(var i=maskBinaryBlockCount;i>=0;i--){<br/> binaryCurrentSubnetBlock=binaryCurrentSubnetBlock+"1";<br/> }<br/> var maxCurrentSubnetBlock=parseInt(binaryCurrentSubnetBlock,2);<br/> <br/> //write the results to the page.<br/> document.getElementById('resMask').innerHTML=mask;<br/> document.getElementById('resNet').innerHTML=net;<br/> document.getElementById('resBC').innerHTML=bc;<br/> document.getElementById('resRange').innerHTML=rangeA + " - " + rangeB;<br/> document.getElementById('resBinIP').innerHTML=ipBin[1]+"."+ipBin[2]+"."+ipBin[3]+"."+ipBin[4];<br/> document.getElementById('resBinMask').innerHTML=maskBinary;<br/> document.getElementById('resBinNet').innerHTML=netBinary;<br/> document.getElementById('resBinBC').innerHTML=bcBinary;<br/> document.getElementById('resClass').innerHTML=standartClass;<br/> document.getElementById('resSubnetId').innerHTML=cidr;<br/> document.getElementById('resMaxHost').innerHTML=maxHost+" possible host(s) in current subnet";<br/> document.getElementById('resMaxNet').innerHTML=maxSubnet+" of total possible subnet, "+maxCurrentSubnetBlock+" possible subnet in current block";<br/> document.getElementById('resImportantBlock').innerHTML=importantBlock;<br/> }else{<br/> alert("invalid value");<br/> }<br/><br/> }<br/> </script><br/></html><br/>[/code]gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com37tag:blogger.com,1999:blog-7546034325993461816.post-90588533271148246292015-12-27T18:00:00.000-08:002016-06-01T17:42:30.499-07:00[FREEWARE] Tomahawk 0.8.4 Music Player<a href="http://asudahlah.com/wp-content/uploads/2015/12/Screenshot-12282015-083756-AM.jpg"><img class="size-medium wp-image-277 aligncenter" src="http://asudahlah.com/wp-content/uploads/2015/12/Screenshot-12282015-083756-AM-300x164.jpg" alt="Screenshot - 12282015 - 08:37:56 AM" height="164" width="300"></a>Tomahawk adalah <a class="zem_slink" href="http://en.wikipedia.org/wiki/Comparison_of_audio_player_software" title="Comparison of audio player software" rel="wikipedia" target="_blank">audio player</a> <a class="zem_slink" href="http://www.wikinvest.com/concept/Open_Source" title="Open Source" rel="wikinvest" target="_blank">open source</a> generasi baru yang menyediakan akses musik dari beberapa sumber. Dapat memainkan lagu-lagu dari library musik lokal Anda, layanan musik berlangganan seperti Spotify dan SoundCloud, atau jaringan <a class="zem_slink" href="http://en.wikipedia.org/wiki/Production_music" title="Production music" rel="wikipedia" target="_blank">library music</a>.<br/><br/>Tomahawk juga dapat menghubungkan Anda dengan komputer lain dan teman-teman untuk berbagi lagu-lagu, Telusuri library musik mereka, dan melihat-lihat playlist milik mereka.<br/><br/>Dengan aplikasi ini, anda juga bisa streaming internet radio, melihat rilis terbaru, dan lagu top dari berbagai tangga lagu musik online.<br/><br/>Tomahawk adalah music player lengkap, yang menggabungkan berbagai sumber musik dan jaringan sosial dalam interface / antarmuka yang mudah digunakan dan intuitif.<br/><ul><br/> <li><a href="http://www.tomahawk-player.org/">Publisher</a></li><br/> <li>Release date : 2015-04-18</li><br/> <li>OS : Windows XP/Vista/7/8</li><br/></ul><br/><a href="http://download.tomahawk-player.org/tomahawk-0.8.4.exe" target="_blank"><img class="size-full wp-image-281 aligncenter" src="http://asudahlah.com/wp-content/uploads/2015/12/asudahlah-btn-download-mirror-1.jpg" alt="asudahlah-btn-download-mirror-1" height="42" width="200"></a>gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com1tag:blogger.com,1999:blog-7546034325993461816.post-56635639724651280052015-12-21T19:35:00.000-08:002016-06-01T17:42:30.472-07:00TP-Link Access Point/router batch reboot/restarter php script [ID]For english version, click <a href="http://asudahlah.com/2015/12/tp-link-access-pointrouter-batch-rebootrestarter-php-script-en/">here</a>.<br/><br/>Jika anda memiliki infrastruktur <a class="zem_slink" href="http://en.wikipedia.org/wiki/Wireless_network" title="Wireless network" rel="wikipedia" target="_blank">wireless network</a>, yg terdiri dari banyak Access point dan <a class="zem_slink" href="http://en.wikipedia.org/wiki/Wi-Fi" title="Wi-Fi" rel="wikipedia" target="_blank">Wi-Fi router</a>. Dan pada suatu ketika, anda harus me-reboot semua <a class="zem_slink" href="http://en.wikipedia.org/wiki/Wireless_access_point" title="Wireless access point" rel="wikipedia" target="_blank">access point</a>. Akan sangat melelahkan jika anda harus login ke setiap AP/Router dan restart secara manual satu persatu.<br/><br/>Ngapain manual kalau kita bisa bikin kode buat nge-restart masal?<br/><br/>Untungnya, dalam kasus saya. semua access point mempunyai merek sama, yaitu <a class="zem_slink" href="http://www.tp-link.com/en" title="TP-Link" rel="homepage" target="_blank">TP-Link</a>. Setelah beberapa riset dan experimen, akhirnya ketemu cara me-reboot access point dan router dengan merk itu melalui <a class="zem_slink" href="http://en.wikipedia.org/wiki/Uniform_resource_locator" title="Uniform resource locator" rel="wikipedia" target="_blank">URL</a>. Lalu saya tulis script <a class="zem_slink" href="http://www.php.net" title="PHP" rel="homepage" target="_blank">PHP</a> untuk membereskan semuanya.<br/><br/>Di infrastruktur saya, saya buat semua access point dan router mempunyai <a class="zem_slink" href="http://en.wikipedia.org/wiki/Internet_Protocol" title="Internet Protocol" rel="wikipedia" target="_blank">IP</a> sub yg berbeda dari <a class="zem_slink" href="http://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol" title="Dynamic Host Configuration Protocol" rel="wikipedia" target="_blank">DHCP</a>. Semua memiliki username dan password sama, semua memiliki metode otentikasi sama.Skrip ini menggunakan bahasa PHP, berjalan di terminal. Jadi mudah untuk diotomatiskan. Tinggal nambahin ke <a class="zem_slink" href="http://en.wikipedia.org/wiki/Cron" title="Cron" rel="wikipedia" target="_blank">cron job</a>. Jangan lupa di "<a class="zem_slink" href="http://en.wikipedia.org/wiki/Chmod" title="Chmod" rel="wikipedia" target="_blank">chmod</a> 777" dan jangan lupa php5-cli sudah terinstall.<br/><br/>Berikut adalah script yg bisa anda copy dan sesuaikan dengan kondisi masing-masing.<br/><br/>[code lang="php"]#!/usr/bin/php<br/>&amp;lt;!--?php<br/>//----------Auth config<br/>$apUser=&amp;quot;admin&amp;quot;;<br/>$apPass=&amp;quot;admin&amp;quot;;<br/>//----------Lists of AP and it's IP address<br/>$apList=Array(<br/>Array(&amp;quot;Ext1&amp;quot;,&amp;quot;192.168.0.15&amp;quot;),<br/>Array(&amp;quot;Ext2&amp;quot;,&amp;quot;192.168.0.17&amp;quot;),<br/>Array(&amp;quot;Jalak&amp;quot;,&amp;quot;192.168.0.2&amp;quot;),<br/>Array(&amp;quot;Barat&amp;quot;,&amp;quot;192.168.0.3&amp;quot;),<br/>Array(&amp;quot;-&amp;quot;,&amp;quot;192.168.0.5&amp;quot;),<br/>Array(&amp;quot;Parkit&amp;quot;,&amp;quot;192.168.0.6&amp;quot;),<br/>Array(&amp;quot;Murai&amp;quot;,&amp;quot;192.168.0.9&amp;quot;),<br/>Array(&amp;quot;Nuri&amp;quot;,&amp;quot;192.168.0.10&amp;quot;),<br/>Array(&amp;quot;Kenari&amp;quot;,&amp;quot;192.168.0.12&amp;quot;),<br/>Array(&amp;quot;Cucak&amp;quot;,&amp;quot;192.168.0.13&amp;quot;),<br/>Array(&amp;quot;Hud-Hud&amp;quot;,&amp;quot;192.168.0.14&amp;quot;),<br/>);<br/><br/>function curl_download($ip){<br/> $Url=&amp;quot;http://&amp;quot;.$ip.&amp;quot;/userRpm/SysRebootRpm.htm?Reboot=Reboot&amp;quot;;<br/> // is cURL installed yet?<br/> if (!function_exists('curl_init')){<br/> die('Sorry cURL is not installed!');<br/> }<br/><br/> // OK cool - then let's create a new cURL resource handle<br/> $ch = curl_init();<br/><br/> // Set URL to download<br/> curl_setopt($ch, CURLOPT_REFERER, &amp;quot;http://&amp;quot;.$ip.&amp;quot;/userRpm/SysRebootRpm.htm&amp;quot;);<br/> curl_setopt($ch, CURLOPT_USERAGENT, &amp;quot;Mozilla/5.0 (X11; Ubuntu; Linux i686;rv:12.0) Gecko/20100101 Firefox/12.0&amp;quot;);<br/> curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br/> curl_setopt($ch, CURLOPT_URL, $Url);<br/> curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);<br/> curl_setopt($ch, CURLOPT_USERPWD, $apUser.&amp;quot;:&amp;quot;.$apPass);<br/> curl_setopt($ch, CURLOPT_TIMEOUT, 120);<br/><br/> $output = curl_exec($ch);<br/><br/> curl_close($ch);<br/><br/> return $output;<br/>}<br/><br/>print &amp;quot;n&amp;quot;;<br/>print &amp;quot;e[1;34mAP Resetter. By Cybermujahidze[0mn&amp;quot;;<br/>print &amp;quot;n&amp;quot;;<br/>print &amp;quot;List of AP:n&amp;quot;;<br/>foreach($apList as $ap){<br/> print str_pad($ap[1],15,' ',STR_PAD_RIGHT).&amp;quot; &amp;quot;.str_pad($ap[0],10,' ',STR_PAD_RIGHT).&amp;quot; queuedn&amp;quot;;<br/>}<br/>print &amp;quot;n&amp;quot;;<br/>print &amp;quot;Resetting ALL APn&amp;quot;;<br/>print &amp;quot;----------------n&amp;quot;;<br/> foreach($apList as $ap){<br/> if(curl_download($ap[1])){<br/> print str_pad($ap[1],15,' ',STR_PAD_RIGHT).&amp;quot; &amp;quot;.str_pad($ap[0],10,' ',STR_PAD_RIGHT).&amp;quot; [ e[1;32mAP Rebootede[0m ]n&amp;quot;;<br/> }else{<br/> print str_pad($ap[1],15,' ',STR_PAD_RIGHT).&amp;quot; &amp;quot;.str_pad($ap[0],10,' ',STR_PAD_RIGHT).&amp;quot; [ e[1;31m AP Down e[0m ]n&amp;quot;;<br/> }<br/> }<br/>print &amp;quot;AP reset finished.n&amp;quot;;<br/>print &amp;quot;------------------n&amp;quot;;<br/>?--&amp;gt;<br/>[/code]gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com2tag:blogger.com,1999:blog-7546034325993461816.post-77295590012722499942015-12-10T05:53:00.000-08:002016-07-27T02:05:22.751-07:00Mengatasi fungsi suspend yang hilang di cinnamon ubuntuSetelah lama menggunakan xfce di laptop ubuntu saya. Akhirnya beberapa hari lalu (karena ditipu guru saya) saya terpaksa mengganti desktop saya menjadi cinnamon.<br/><br/>Selama ini, saya tidak pernah mematikan / shut down laptop saya. Setiap saya selesai menggunakan, selalu saya suspend agar tidak perlu menunggu booting saat ingin menggunakan lagi.<br/><br/>Nah, muncul sebuah masalah setelah saya menginstall cinnamon, yaitu tidak ada menu suspend ketika klik quit di launcher. Yang muncul hanya tombol "Restart", "Cancel" dan "Shut Down".<br/><br/>Pada kasus saya, yang menjadi sumber masalah adalah settingan cinnamon session yang default-nya tidak menggunakan logind. Untuk menggeceknya, bisa anda gunakan perintah:<br/><br/><code>gsettings get org.cinnamon.desktop.session settings-daemon-uses-logind</code><br/><br/>gsettings get org.cinnamon.desktop.session session-manager-uses-logind<br/><br/>apabila kedua setting diatas menghasilkan "false". Maka artinya cinnamon session tidak menggunakan logind. Kita harus mensetnya secara manual dengan perintah:<br/><br/><code><br/>gsettings set org.cinnamon.desktop.session settings-daemon-uses-logind true<br/>gsettings set org.cinnamon.desktop.session session-manager-uses-logind true<br/>gsettings set org.cinnamon.desktop.session screensaver-uses-logind false</code><br/><br/>setelah restart, tombol suspend sekarang muncul di window session<br/><br/><a href="http://asudahlah.com/wp-content/uploads/2015/12/Screenshot-12102015-013304-PM.jpg"><img class="alignnone size-medium wp-image-246" src="http://asudahlah.com/wp-content/uploads/2015/12/Screenshot-12102015-013304-PM-300x118.jpg" alt="Screenshot - 12102015 - 01:33:04 PM" width="300" height="118" /></a><br/><br/>Akhirnya sekarang saya sudah bisa suspend. :)<br/><br/>Tapi disini saya menemui masalah lagi, yaitu ketika laptop lid saya tutup, ubuntu saya tidak mau suspend. Padahal di power setting sudah saya atur demikian.<br/><br/>Akhirnya saya menemukan solusi, yaitu dengan cara uncomment pada satu baris konfigurasi di file:<br/><blockquote>/etc/systemd/logind.conf</blockquote><br/>gunakan text editor kesayangan anda untuk merubahnya, jangan lupa perintah sudo.<br/><br/><code>sudo nano /etc/systemd/logind.conf</code><br/><br/>cari baris yang memuat konfigurasi "<strong>HandleLidSwitch=suspend</strong>", kemudian hilangkan tanda pagar diawal baris. (uncomment).<br/><br/>Lalu restart, dan hilanglah masalah "suspend" di laptop ubuntu cinnamon saya.<br/><br/>Setelah itu, masih banyak lagi masalah yang saya temui saat masa transisi ke cinnamon. yang akan saya share di artikel lain.<br/><br/>Sekian coretan singkat saya, mudah-mudahan bisa berguna.<meta http-equiv='refresh' content='0;url=http://id.asudahlah.com/2016/07/mengatasi-fungsi-suspend-yang-hilang-di.html'>gemahttp://www.blogger.com/profile/03684781746845626482noreply@blogger.com0