I am trying to generate a pdf file via the dompdf library (the displayed values come from a mysql database), and I have an encoding problem. I did a lot of research but without success.
The problem is that at each line break, a "/ n" is displayed.
I tried to use different functions html_entities_decode, nl2br and even str_replace ("/ n", "", $ str), but could not delete it.
The "/ n" is not registered in the database at the specified value. I looked in the source files dompdf and it does not come from there either.
A big thank you in advance for your responses !
CODE
use Barryvdh\DomPDF\Facade as PDF; // Barryvdh use dompdf
// function used
public function downloads($from, $type, $unique_id, $downloads, $dom, $do) {
$data['from'] = $from;
$data['type'] = $type;
$data['unique_id'] = $unique_id;
$data['title'] = 'PDF Generation';
//User Details
$userController = new userController;
$data['logeduser'] = $userController->logeduser();
$userpermissions = $userController->getpermissions();
//Notification Details
$notification = new notificationModel;
$data['notification_list'] = $notification->getAllRemainder();
//PDF Generation
$sales = new salesModel();
$data['offer_sav'] = $sales->getOfferSAVDetails($unique_id);
$data['offer_sav_products'] = $sales->getOfferSAVProductDetails($unique_id);
$data['offer_eg'] = $sales->getOfferSAVDetails($unique_id);
$data['offer_eg_component'] = $sales->getOfferEGComponent($unique_id);
$data['offer_eg_accessory'] = $sales->getOfferEGAccessory($unique_id);
$data['offer_eg_options'] = $sales->getOfferEGOptions($unique_id);
$data['sav_eg_without_sm'] = $sales->getSAVEGWithoutSM($unique_id);
//view()->share($data);
//return view('sales/pdfonly', $data);
$pdf = PDF::loadView('sales/pdfonly', $data);
//$view = view('sales/pdfonly', $data);
//$pdf = PDF::loadHTML($view);
if(!empty($_GET['print']))
return $pdf->stream('pdf_'.$from.'_'.$type.'.pdf', array("Attachment" => 0));
else
return $pdf->download('pdf_'.$from.'_'.$type.'.pdf');
}
// exemple of code output
{!! $eg->sales_offer_note2 !!} // = remarque1 <div>r<br> remarque2</div><div><br></div><div><br></div><div>
€
/*
OUTPUT
remarque1
r/n
emarque2
/n
/n
€
/n
*/
Related
I am using dompdf to generate invoices, one invoice is generated but I want to generate 3 copies of same invoice pdf file which is copy1, copy2, copy3. Each copy should start on new page of the same pdf file.
$dompdf = PDF::loadView('admin.pdf.invoice', compact('data'));
$options = [];
$options['isHtml5ParserEnabled'] = true;
$options['isRemoteEnabled'] = true;
$options['isPhpEnabled'] = true;
$dompdf->setOptions($options);
$dompdf->setPaper('A4', 'portrait');
$file =LOCAL_PDF_PATH."invoice.pdf";
$dompdf->save(LOCAL_PDF_PATH."invoice.pdf");
Try this code
for($i=0;$i<3;$i++){
$dompdf = PDF::loadView('admin.pdf.invoice', compact('data'));
$options = [];
$options['isHtml5ParserEnabled'] = true;
$options['isRemoteEnabled'] = true;
$options['isPhpEnabled'] = true;
$dompdf->setOptions($options);
$dompdf->setPaper('A4', 'portrait');
$file =LOCAL_PDF_PATH."invoice.pdf";
$dompdf->save(LOCAL_PDF_PATH."invoice".$i.".pdf");
}
Use array chunk for that inside your view
$columns = 3; // number of columns on a page
// calculate number of rows and break the data into chunks
$num_rows = ceil(count($data) / $columns);
$data = array_chunk($data,$num_rows);
my RESTapi is outputting following json string
{"chat":[{"id":100,"chat_id":38,"created_at":"2016-09-07 08:48:17","updated_at":"2016-09-07 08:48:17","messageContent":"Hi there","sender":"client"},{"id":101,"chat_id":38,"created_at":"2016-09-07 08:48:29","updated_at":"2016-09-07 08:48:29","messageContent":"hello sir","sender":"admin"},{"id":102,"chat_id":38,"created_at":"2016-09-07 09:14:24","updated_at":"2016-09-07 09:14:24","messageContent":"test","sender":"client"},{"id":103,"chat_id":38,"created_at":"2016-09-07 09:16:06","updated_at":"2016-09-07 09:16:06","messageContent":"test","sender":"client"}],"currentChatId":38,"senderName":"Client name"}
Notice the sender column. These are all coming from db. Now I am writing an algorithm which checks this and finds the sender name to the view instead of just sender type.
I am having problem regarding this..any suggestion?
Here is the controller function
public function getAllChat(Request $request)
{
// $chatList = chatMessage::where();
$clientId = $request->session()->get('userId');
//current chat id7
$currentChatId = $request->session()->get('currentChatId');
//find who sent it admin/client
$chatMessageList = chatMessage::where('chat_id',$currentChatId)->get();
//sender
foreach($chatMessageList as $cht)
{
//find out sender
$sender = $cht->sender;
if($sender=="client")
{
$chtP = chatParticipants::find($currentChatId)->first();
$clientId = $chtP->client_id;
//find client name
$client = Client::find($clientId);
$ch->sender = $client->name;
}
elseif($sender=="admin")
{
$chtP = chatParticipants::find($currentChatId)->first();
$adminId = $chtP->admin_id;
//find client name
$admin = Admin::find($clientId);
$name = $admin->name;
$ch->sender = $admin->name;
}
}
return response()->json([
"chat"=> $chatMessageList,
"currentChatId" => $currentChatId,
"senderName"=>$name
]);
}
Error : Creating default object from empty value empty value
You don't have $ch variable defined anywhere - I guess you wanted to use $cht variable instead.
Replace
$ch->sender = $client->name;
with
$cht->sender = $client->name;
and
$ch->sender = $admin->name;
with
$cht->sender = $admin->name;
It was a typo.
$cht->sender=$name;
instead of
$ch->sender=$name;
I need a help with this one. I use PHP and dompdf to create this invoice in Cyrillic:
On pdf everything is looking fine, but when I try to print it, I get it like:
and I get also one extra blank page added. Do you know how this can be fixed?
The example can be seen at: http://projects.stanislavstankov.com/php-pdf/pdfview.php
and sourse downloaded at: http://projects.stanislavstankov.com/php-pdf/php-pdf.rar
PHP code:
<?php
$data = file_get_contents("HTML Invoice Template.html");
$data = iconv('UTF-8//IGNORE','UTF-8//IGNORE', $data);
$data = preg_replace('/<body>/', "", $data);
$data = preg_replace('/<\/body>/', "", $data);
$data = preg_replace('/<html>/', "", $data);
$data = preg_replace('/<\/html>/', "", $data);
// inhibit DOMPDF's auto-loader
define('DOMPDF_ENABLE_AUTOLOAD', false);
//include the DOMPDF config file (required)
require 'extensions/dompdf/dompdf_config.inc.php';
//if you get errors about missing classes please also add:
require_once('extensions/dompdf/include/autoload.inc.php');
//$data = mb_convert_encoding($data, 'HTML-ENTITIES', 'UTF-8');
//$data = utf8_decode($data);
//$data = iconv('Windows-1251','UTF-8', $data);
//
//generate some PDFs!
$dompdf = new DOMPDF(); //if you use namespaces you may use new \DOMPDF()
$dompdf->set_paper('a4', 'portrait');
$dompdf->load_html($data);
//$dompdf->set_paper(array(0,0,595,842));
$dompdf->render();
$dompdf->stream($log_id.".pdf", array("Attachment"=>0));
?>
here is my function in a class called getminecraftprofile.php
basically it takes data from https://sessionserver.mojang.com/session/minecraft/profile/
dependant on the UUID of the user and gets an array that i can pick formation from.
the URL strings are base64 encoded so it decodes that as well.
public function getSkin()
{
$properties = $this->properties;
$encoded = json_encode($properties);
$imgUrl = json_decode(base64_decode(json_decode($encoded,true)[0]['value']),true) ['textures']['SKIN']['url'];
$img = file_get_contents($imgUrl);
echo($img);
}
this is the file that is passing in what user profile to find and get the skin of that profile. for most profiles it works perfectly and you can see it in action at http://jamesplanet.net/test/src/getskin.php?username=dinnerbone
<?php
include ($_SERVER["DOCUMENT_ROOT"] . '/test/src/getminecraftprofile.php' )?>
<?php
$profile = ProfileUtils::getProfile("useruser3");
//$result = $profile->getSkin();
$properties = $profile->getProperties();
$encoded = json_encode($properties);
$imgUrl = json_decode(base64_decode(json_decode($encoded,true)[0]['value']),true)['textures']['SKIN']['url'];
$img = file_get_contents($imgUrl);
var_dump($encoded);
echo('Texture URL Decoded: ' . $imgUrl );
it all grinds to a halt however if i say
$profile = ProfileUtils::getProfile("_jeb");
then it throws this at me
Warning: file_get_contents(): Filename cannot be empty in /Applications/AMPPS/www/test/src/getskin.php on line 10
string(908) "[{"name":"textures","value":"eyJ0aW1lc3RhbXAiOjE0MDM0ODE0Mjk2NDMsInByb2ZpbGVJZCI6IjdmM2ZjY2ZjYzUzOTQzNTdhZmEwMjExYTYyODc3MWU0IiwicHJvZmlsZU5hbWUiOiJfamViIiwiaXNQdWJsaWMiOnRydWUsInRleHR1cmVzIjp7fX0=","signature":"xIgM9w3MfZHMjlsZyrMDAF1CTAhhhhSWfjRqJgW3NNTCyotVkL9IMDN6ZzPIoNESFBlhwspxfKhrAC81+GlEUcFxLYilzO6qE+\/pYciNS78b9MxRK7R5xEoNNzFhXAyo9\/cW5X7V6bFxOB2MU9Fg7NXt\/B\/u4VypqGsbCA+OAm7Kgomlhkw3wkm1R1djfd5oBqwlCKhTWxt4k4mQJzThsqE5ffLdTcPeMoipezw4NHAV1QJfXZZlGsEDG\/KkzUDh6eOz+QufYvhcxGrOAfwJ4TzlLbu0QyT8IRIu0E5LY96sTSecWQ27W7TzsumXiXrkpOSLZrOi5yqGFMmfOEHqo8dLRnxhoNmyQpzN2rE4OvNBwKqvzOeRbigOzQSSg5xupydFFLdc20Pj9CXcTB18K12fXt6W8FJ+AzYUccPrSx\/1RJALkef9W5DCxNLOLLR5yIS8Irff3\/Gn2sRwTG8EXJz+6qRB50e02jdyTv9DZz853w+TAVoNtSkBB8W9aC08XUSCFFg1KAaLILkRi8H+76BE1pZKraPAoW8MpsUbd6J+6k0kU5lqdtiJZ1PtDssVMA+p9MwtOmQqD7n4hPXehULBv4+phQjZi37s1I6ZWYrQBE7raq3ZKfDx9ztccmHEyk\/VHSboo0QmtM8iCOCOC1l0Jv9QrV\/YQRZil03x+OQ="}]" Texture URL Decoded:
i know there is data there because its visible before the base64 decode.
where is it going wrong?
here is an example of what is being parsed
{"id":"069a79f444e94726a5befca90e38aaf5","name":"Notch","properties":[{"name":"textures","value":"eyJ0aW1lc3RhbXAiOjE0MDM0ODE0MjIwNDUsInByb2ZpbGVJZCI6IjA2OWE3OWY0NDRlOTQ3MjZhNWJlZmNhOTBlMzhhYWY1IiwicHJvZmlsZU5hbWUiOiJOb3RjaCIsImlzUHVibGljIjp0cnVlLCJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYTExNmU2OWE4NDVlMjI3ZjdjYTFmZGRlOGMzNTdjOGM4MjFlYmQ0YmE2MTkzODJlYTRhMWY4N2Q0YWU5NCJ9LCJDQVBFIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvM2Y2ODhlMGU2OTliM2Q5ZmU0NDhiNWJiNTBhM2EyODhmOWM1ODk3NjJiM2RhZTgzMDg4NDIxMjJkY2I4MSJ9fX0=","signature":"TJieaNbZrHGmA7V3n8jn0V/ZVPXAy4EK44lxsh3kidK4JML8BEEZZjt/1eixbbXHGMH39e122UO7ehX2PIutuIIK7ZhvAo6wy8yMG34rPxjuyg0jzZe/tZjKv/kq7Bi78m6PErfwBsaUxjkCD3J0sqLXyvOy9rQ2CbYGKTxnVtOZGzxIaQqC5QH/2We704cMUJ3wIgN9FckppVA2E+DYu0G8iFlvYQnjxxIclokv3jOcPrYwVq0COWJaDrGd0+o+dKUfTfiT/k1OLBuceGDmqjb9CmcbEeTFFRJrmA6XEoA6OHZbLOMpIPLtEs8LbZqBH38Cuf+izPGLl1e28bITwPUdpABzvxaxKoYB3XYnWvtAqyfvzsCEgoBVnV7psx1W+kWU9xjCxC591457bollR2KVrSQBv7p0KBm4qVaVPKa01bjcMfcDDx5PRBIcL6XECVmKoeqh+qUrrf1Z9WN0l4OEujmRd8/tbJMjtJTdqCUZ5ak7urJHl8iTNNLqjx/Dsi47DeZvfUGdSC6ivpDIDBQv3myBLNnHqE7/+Z8DGKa+Bf/nQW0sMdVsNFOxIeiXMyXpWAHMw3/Ee21aMWEUq+rD8l4CD2FTZkQ/gv18y3y7CR9fYIKU+EPccnDUtyF+Y257UtCUag7i0vlkiBpIh46jhpjIkhR+iAG4URqQA6I="}]}
Hello: I have a web form that submits data to my db. I am able to create a signature image and save it within my directory. I want to save/store that signature image in my mysql db along with the record so I can call it later.
Written in CodeIgniter 2.0
here are my model and controller.
public function sig_to_img()
{
if($_POST){
require_once APPPATH.'signature-to-image.php';
$json = $_POST['output'];
$img = sigJsonToImage($json);
imagepng($img, 'signature.png');
imagedestroy($img);
$form = $this->input->post();
var_dump($form);
$this->coapp_mdl->insert_signature($form);
}
}
public function insert_signature($data)
{
$sig_hash = sha1($data['output']);
$created = time();
$ip = $_SERVER['REMOTE_ADDR'];
$data = array(
'first_name' => $data['fname'],
'last_name' => $data['lname'],
'signator' => $data['name'],
'signature' => $data['output'],
'sig_hash' => $sig_hash,
'ip' => $ip,
'created' => $created
);
return $this->db->insert('signatures', $data);
}
I found the function below on php.net but apparently I am doing something wrong or various things wrong. Does anyone know how to accomplish this functionality?
$imagefile = "changethistogourimage.gif";
$image = imagecreatefromgif($imagefile);
ob_start();
imagepng($image);
$imagevariable = ob_get_contents();
ob_end_clean();
Got it - For those curious here are the changes to my controller:
public function sig_to_img()
{
if($_POST){
require_once APPPATH.'signature-to-image.php';
$json = $_POST['output'];
$img = sigJsonToImage($json);
// Save to file
$file_name = trim(str_replace(" ","_",$_POST['name']));//name to used for filename
imagepng($img, APPPATH."../images/signatures/".$file_name.'.png');
$sig_name = $file_name.'.png'; //pass to model
// Destroy the image in memory when complete
imagedestroy($img);
$form = $this->input->post();
var_dump($form);
$this->coapp_mdl->insert_signature($form, $sig_name);
}
}