Best Practice to Print Barcode In dompdf - php

I want to create a label sticker with barcode in PDF.
Please give me the suggestion to create this things.
Now , I use CI3 and dompdf.
I have tried googling a lot of time, but I am not found the best practice to realized this thing.
I followed your suggestion,
Now I have folder in vendor/Piqcer.
I try this into my view_report like this:
<tr>
<td class="solid" colspan="2">
<?= $value->NO_URUT ?>
<?php
$generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
echo '<img src="data:image/png;base64,' . base64_encode($generator->getBarcode($value->NO_URUT, $generator::TYPE_CODE_128)) . '">';
?>
</td>
</tr>
But I got error :
Fatal error: Class 'Picqer\Barcode\BarcodeGeneratorPNG' not found
UPDATE
So, this library is on ci_folder\vendor\picqer\php-barcode-generator.
Now I create an autoload library like this :
class Barcode {
public function generate($id) {
require_once("./vendor/picqer/php-barcode-generator/src/BarcodeGeneratorJPG.php");
$generator = new Picqer\Barcode\BarcodeGeneratorJPG();
echo $generator->getBarcode($id, $generator::TYPE_CODE_128);
}
}
IN my controller :
$this->load->library('dompdfgenerator');
$this->load->library('barcode');
$data = array(
'result' => $result,
'container' => $containers
);
$html = $this->load->view('members/megumi/check_list_per_tanggal/v_laporan_sticker_pilihan', $data, true);
$this->dompdfgenerator->generate($html, "$identity");
Now in view :
<tr>
<td class="solid" colspan="2">
<?= $value->NO_URUT ?>
<?php
echo '<img src="data:image/png;base64,' . base64_encode($this->barcode->generate($value->NO_URUT)) . '">';
?>
</td>
</tr>
It still : Fatal error: Class 'Picqer\Barcode\BarcodeGenerator' not found
Please advise...

Have you tried using libraries, such as PHP Barcode Generator. It will generate a barcode for you and export it as an SVG, PNG or JPG or HTML. You should just insert the resulting barcode to the HTML you wil lbe exporting with DOMPDF.

Related

phpFlickr - multiple albums on one page not working - single album works

I'm trying to implement the phpFlickr-wrapper (https://github.com/dan-coulter/phpflickr) in my MODX-project.
I tried a simple code i found here on stackoverflow and put it in a snippet called "Flickr":
//include the core file
if(!class_exists("phpFlickr")) require_once './assets/phpflickr-master/phpFlickr.php';
// include the config file
require_once('./assets/phpflickr-master/config.php');
$f = new phpFlickr($key, $api_secret);
$mySetID = $album;
$mySet = $f->photosets_getPhotos($mySetID, NULL, NULL);
foreach ($mySet['photoset']['photo'] as $photo) {
echo '<div><img src="'. $f->buildPhotoURL($photo, 'medium') .'" alt="" /></div>';
}
The snippet call:
[[!Flickr? &setname=`[[+tv.FickrID]]`]]
The TV "FlickrID" holds the ID of the wanted album.
This is working fine for one album. But when I try to output a second gallery the output gets stopped after the first snippet-call. The second time the snippet-call is made it stops with this entry in the MODX error-log:
...75.include.cache.php : 18) PHP warning: Invalid argument supplied for foreach()
How can I display more than one album on the same page, any idea?
Problem solved...
I just had to include the content from the "config.php"-file inside the snippet, rather than referring to it by the "require_once"-line of code.
<?php
$album = $modx->getOption('setname',$scriptProperties,'');
//include the core file
if(!class_exists("phpFlickr")) require_once './assets/phpflickr-master/phpFlickr.php';
if(!class_exists("PEAR")) require_once './assets/phpflickr/PEAR/PEAR.php';
$key = "<flickr-api-key>";
$api_secret = "<flickr-api-secret>";
$username="<flickr-username>";
$f = new phpFlickr($key, $api_secret);
$f->enableCache("fs", "./assets/phpflickr-master/cache");
$mySetID = $album;
$mySet = $f->photosets_getPhotos($mySetID, NULL, NULL);
foreach ($mySet['photoset']['photo'] as $photo) {
$output .= '<div><a rel="lightbox[]" href="'. $f->buildPhotoURL($photo, 'large') .'"><img src="'. $f->buildPhotoURL($photo, 'medium') .'" alt="" /></a></div>';
}
return $output;

MySQL Insert image in php the post functions return 0

Im trying to import a profile picture for my users and insert it in the database in blob format. But when im running the code and i select an image i get file_get_contents() cannot be empty error
I have tried many solutions from other post related to this problem none of them worked.
Controller:
public function wijzigen()
{
$foto = $this->input->post($_FILES['profielfoto']['tmp_name']);
$fotoContent = addslashes(file_get_contents($foto));
$id = $this->session->userdata('id');
$gebruiker = new stdClass();
$gebruiker->id = $id;
$gebruiker->profileImage = $fotoContent;
$this->load->model('gebruiker_model');
$this->gebruiker_model->update($gebruiker);
Model:
function update($gebruiker)
{
$this->db->where('id', $gebruiker->id);
$this->db->update('gebruiker', $gebruiker);
}
View:
<?php
$attributes = array('name' => 'mijnFormulier', 'enctype'=>'multipart/form-data', 'method'=>'post', 'accept-charset'=>'utf-8');
echo form_open('Home/wijzigen', $attributes);
?>
<table>
<tr>
<td><?php echo form_label('Profiel Foto:', 'profielfoto'); ?></td>
<td><input type="file" name="profielfoto" id="profielfoto" required accept=".png"/></td>
</tr>
<tr>
<td></td>
<td>
<?php echo form_submit('knop', 'Wijzigen', 'class = "btn btn-login login-formcontrol"'); ?>
</td>
</tr>
</table>
<?php echo form_close(); ?>
Error:
Severity: Warning
Message: file_get_contents(): Filename cannot be empty
Filename: controllers/Home.php
Line Number: 147
Backtrace:
File: /home/arne/Desktop/WebApplication/CodeIgniter/application/controllers/Home.php
Line: 147
Function: file_get_contents
File: /home/arne/Desktop/WebApplication/CodeIgniter/index.php
Line: 315
Function: require_once
I except an image to be in my database but the post isn't working
Thanks,
$_FILES and $_POST vars are 2 different things.
$foto = $this->input->post($_FILES['profielfoto']['tmp_name']);
should be:
$foto = $_FILES['profielfoto']['tmp_name'];
and please note this does not take in to account errors so you should probably check for that as a matter of course.

PHP Simple HTML DOM Parser works on localhost but not live WordPress website

I am working with a WordPress plugin that outputs a table of data on the plugin's admin page. I have modified the plugin to add a button on the page that exports the table of data into a CSV file. Everything works perfectly on my localhost, but after transferring to the live server, it no longer works - the CSV file that is exported is blank. I am using PHP Simple HTML DOM Parser to parse the HTML table and convert it to CSV, and this seems to be where the roadblock is happening.
I have added the export button with this code (parts of the URL hidden for privacy):
<form action="**website-url**/wp-content/plugins/**plugin**/includes/votes-logs-export.php" method="POST">
<input type="submit" value="<?php _e('Export Table as CSV','**plugin**'); ?>" class="button" id="export" name="export" />
</form>
My file votes-logs-export.php has this code:
require_once('../../../../wp-load.php');
include 'simple_html_dom.php';
$file_name = "contest_votes_".date('d-m-Y-H-i-s').'.csv';
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=".$file_name);
global $wpdb;
$export_sql = "SELECT log.*,pst.post_title FROM ".$wpdb->prefix."votes_tbl as log LEFT JOIN ".$wpdb->prefix."posts as pst on log.post_id=pst.ID ORDER BY pst.post_title DESC";
$export_logs = $wpdb->get_results($export_sql, OBJECT);
if (!empty($export_logs)) {
$fp = fopen("php://output", "w");
if ($wpdb->num_rows > 0) {
$output = '';
foreach ($export_logs as $logs) {
$vote_id = $logs->post_id;
$postdata = get_posts($vote_id);
$vote_author_id = $postdata[0]->post_author;
$vote_author = get_the_author_meta( 'display_name', $vote_author_id );
$voter_name = $logs->ip;
if(filter_var($voter_name, FILTER_VALIDATE_IP) !== false)
$voter_name = $logs->ip;
else
$voter_name = get_the_author_meta( 'display_name', $voter_name );
$voted_date = $logs->date;
$output .= '
<tr>
<td>'.$logs->post_title.'</td>
<td class="author column-author">'.$vote_author.'</td>
<td class="author column-author">'.$voter_name.'</td>
<td class="author column-author">'.$voted_date.'</td>
</tr>';
}
}
$table = '
<table>
<tr>
<th>Title</th>
<th>Author</th>
<th>Voter</th>
<th>Vote Date</th>
</tr>' . $output . '</table>';
$html = str_get_html($table);
foreach($html->find('tr') as $element){
$td = array();
foreach( $element->find('th') as $row){
$td [] = $row->plaintext;
}
// prevent blank rows being inserted into CSV file
if (!empty($td)) {
fputcsv($fp, $td);
}
$td = array();
foreach( $element->find('td') as $row){
$td [] = $row->plaintext;
}
// prevent blank rows being inserted into CSV file
if (!empty($td)) {
fputcsv($fp, $td);
}
}
fclose($fp);
}
I'm able to view the PHP page in order to debug by commenting out the header redirects. On my localhost, the PHP page outputs the table data in CSV format, but on the live server, it's a blank page. (No errors on the page or in error_log.) I have debugged this code and found that $table and all of the code above it seems to be running perfectly - if I print the contents of $table I get a formatted HTML table of my data:
<table>
<tbody>
<tr>
<th>Title</th>
<th>Author</th>
<th>Voter</th>
<th>Vote Date</th>
</tr>
<tr>
<td>Writing-on-Stone Park</td>
<td class="author column-author">admin</td>
<td class="author column-author">email#email.com</td>
<td class="author column-author">2014-07-04 16:42:57</td>
</tr>
<tr>
<td>White Tailed Ptarmigan</td>
<td class="author column-author">admin</td>
<td class="author column-author">email#email.com</td>
<td class="author column-author">2014-07-04 06:07:51</td>
</tr>
<tr>
<td>Rockbound Lake</td>
<td class="author column-author">admin</td>
<td class="author column-author">email#email.com</td>
<td class="author column-author">2014-07-04 06:07:28</td>
</tr>
</tbody>
</table>
The block happens at $html = str_get_html($table) - if I print out $html, nothing gets printed to the page, not even an error or any kind of 'false' or 'NULL' value. If I try to echo a string after that block of code, it is not output to the page, so the script seems to stop running there.
I was able to confirm that my simple_html_dom.php file is being referenced correctly because echoing a string in that file does get output to my votes-logs-export.php page. Yet the functions within the file don't seem to be firing? I'm out of ideas on this one!
I confirmed that 'allow_url_fopen' is set to 'true' in php.ini. In my simple_html_dom.php file, I also tried changing the "MAX_FILE_SIZE" from 600,000 to 100,000,000 but to no effect. The memory on the server is set to 40MB. I admit I am a newbie with working with custom WordPress plugins, but the fact this works on my localhost seems to indicate there is some kind of configuration or compatibility issue on the live server...? I would appreciate any guidance or tips you can offer.
PHP version on the live server is 5.4.21. My localhost is using 5.3.
I ended up explicitly setting error_reporting(1) in my file, which finally threw me a valuable error - Call to undefined function mb_detect_encoding() on line 1238 of simple_html_dom.php. Turns out I needed to enable the PHP extension mbstring on my server. Problem solved!

How to include image in html->pdf in php class

How can i include an image in an html->pdf class. Here is the code I'm currently working for:
<?php
session_start();
class PrintSomething
{
function requestPrintSomething()
{
ob_start();
$root = "../";
$content = "";
include $root."assets/library/html2pdf/html2pdf.class.php";
$content .= "<img source = ../assets/images/Some_logo.PNG>";
$content .= "<p>Something Date on " . $date . " at ".$time .":00 </p>";
$content .= "
<tr>
<td> ".$somethinga ."</td>
<td> ".$somethingb."</td>
</tr>
";
$content .= "
</table>
";
$html2pdf = new HTML2PDF('L');
$html2pdf->WriteHTML($content);
$html2pdf->Output('Something List '.date("Y-m-d").'.pdf');
exit;}}?>
some info regarding the library:/**
* HTML2PDF Librairy - main class
*
* HTML => PDF convertor
* distributed under the LGPL License
*
* #author Laurent MINGUET <webmaster#html2pdf.fr>
* #version 4.03
*/
I hope someone know how to include image because I can't figure it out. I already tried the image source in html but somehow I've got a fatal error. Thanks in advance!
That img tag is not valid, src is the correct attribute to use.
$content .= "<img src='http://your_full_path/assets/images/Some_logo.PNG'>";
Also use your full path to the image.

Image tag src attribute not working in wordpress

I am creating a Google chart in wordpress, chart is rendered and stored into an image. However I am not able to call the image using <img src="" />. Following is the code:
$filepath = "/wp-content/uploads/graph.png";
file_put_contents($filepath, $response);
echo $filepath;
echo "<img src=\"/wp-content/uploads/graph.png\">";
I have also tried with http://*/graph.png which is not working. If I open the same in different browser, image is showing properly.
Try this
echo '<img src="' . get_bloginfo('template_directory') . '/images/logo.gif" />';
Go for 'template_directory' or 'stylesheet_directory'.
You may try the below code;
Much better if you combine your HTML code and PHP code. Much cleaner easy to read.
PHP code
<?php
Try changing your file path to
$filepath = "../../wp-content/uploads/graph.png";
file_put_contents($filepath, $response);
?>
HTML Code
<img src="<?php echo $filepath; ?>"/>

Categories