include jquery signature into PDF with FPDF - php

I'm using this jquery plugin to capture or draw a signature.
http://keith-wood.name/signature.html
And FPDF php plugin to generate pdf
Now I must include signature into pdf...
With FPDF I can insert an image, but I have problem exporting signature to jpeg/png (I don't even know if is possibile)
How I can do this

On the page you gave link to (this one) in Save/Restore tab you have possibility to save it as a base64 encoded picture (jpeg or png).
So you can use that and to save base64 encoded picture as a file here is a solution
The problem is that data:image/png;base64, is included in the encoded contents. This will result in invalid image data when the base64 function decodes it. Remove that data in the function before decoding the string, like so.
function base64_to_jpeg($base64_string, $output_file) {
// open the output file for writing
$ifp = fopen( $output_file, 'wb' );
// split the string on commas
// $data[ 0 ] == "data:image/png;base64"
// $data[ 1 ] == <actual base64 string>
$data = explode( ',', $base64_string );
// we could add validation here with ensuring count( $data ) > 1
fwrite( $ifp, base64_decode( $data[ 1 ] ) );
// clean up the file resource
fclose( $ifp );
return $output_file;
}
To clarify variable $base64string is a string that in your case is generated by plugin, and $output_file is a filpath where to save the picture.

Related

json_encode is adding extra square brackets - PHP

json_encode is adding extra square bracket when I write code in to the json file using php.
METHOD to create & write JSON file.
function appendData($data){
$filename='data.json';
// read the file if present
$handle = #fopen($filename, 'r+');
// create the file if needed
if ($handle === null)
{
// $handle = fopen($filename, 'w+');
$handle = fopen($filename, 'w') or die("Can't create file");
}
if ($handle)
{
// seek to the end
fseek($handle, 0, SEEK_END);
// are we at the end of is the file empty
if (ftell($handle) > 0)
{
// move back a byte
fseek($handle, -1, SEEK_END);
// add the trailing comma
fwrite($handle, ',', 1);
// add the new json string
fwrite($handle, json_encode($data,JSON_UNESCAPED_SLASHES) . ']');
}
else
{
// write the first event inside an array
fwrite($handle, json_encode(array($data),JSON_UNESCAPED_SLASHES));
}
// close the handle on the file
fclose($handle);
}
}
Method call with data array argument
$file= appendData($data);
Data
$data= array(
'name' => "abc",
'image_url' => "cdf",
);
The JSON Output comes like
[[{"name":"Apple iPhone 6S\u00a0with FaceTime\u00a0- 32GB, 4G LTE, Gold","image_url":"https://m.media-amazon.com/images/I/51jV7zsrOtL._AC_UL436_.jpg"}]]
Problem: There are extra square brackets appending in json output, that seems fine becuase of using json_encode(array($data)) .
But it doesn't parse on front end using javascript or jquery.
Question: how to parse this double square JSON data using jquery or how to append data properly in json file using php?
I see no problem with your output. You are adding an unnecessary layer of array by json_encode(array($data)) but you just have to factor that into your JS when you try to access the values. You can access it as a 2-d array of objects as in this snippet:
let json = '[[{"name":"Apple iPhone 6S\u00a0with FaceTime\u00a0- 32GB, 4G LTE, Gold","image_url":"https://m.media-amazon.com/images/I/51jV7zsrOtL._AC_UL436_.jpg"}]]';
let v = JSON.parse(json);
console.log(v[0][0].name);
console.log(v[0][0].image_url);

String showing special characters as it was urlencoded

I've got a site where the user can upload a csv file with some information and I read and save that information in the database. The problem is, the user sometimes report that the information he/she uploads appears with "strange characters" in the browser and I've checked and it was saved in the database the same way (with "strange characters). So I asked for the file he/she used and upload from my computer and everything went fine. The user uploaded from a Mac computer and I uploaded from a Windows computer. The server with the site is Windows.
Here's a print of the error the user describes:
I can upload that same csv and that string stays ok. I noticed the "strange characters" are codes for url's. For example the space is replaced by %20 in the print. But this string comes from a csv file that apparently is ok. And as I can't reproduce the error it's hard to understand what could be happening. But all seems to be some kind of encoding problem between Mac-Windows. Do you ever got this king of problem?
EDIT:
Here's the code that handles the csv file:
while ($valor = fgetcsv ($handle, filesize($T2), ";")){
for ($i=0;$i<$conta;$i++){
$cv=$data[$i];
$row[$cv]=trim($valor[$i]);
}
if ($row['Ano']==$sql_Documento['Ano'] && $sql_Documento['NumDocumento']==$row['Numero']){
$count++;
$campos = array();
$campos['IdDocumento'] = $sql_Documento[IdDocumento];
$campos['Sociedade'] = mssql_escape_string($row['Sociedade']); //this is the field that gives me problems
$campos['DocumentoId'] = $sql_Documento[DocumentoId];
$campos['page_dr'] = $row['Pagdr'];
$campos['page_file'] = $row['Pagpdf'];
$DocumentoId = insertNewRecord("DocSoc",$campos);
}
}
The mssql_escape_string is this function:
function mssql_escape_string($data) {
if ( !isset($data) or empty($data) ) return '';
if ( is_numeric($data) ) return $data;
$non_displayables = array(
'/%0[0-8bcef]/', // url encoded 00-08, 11, 12, 14, 15
'/%1[0-9a-f]/', // url encoded 16-31
'/[\x00-\x08]/', // 00-08
'/\x0b/', // 11
'/\x0c/', // 12
'/[\x0e-\x1f]/' // 14-31
);
foreach ( $non_displayables as $regex )
$data = preg_replace( $regex, '', $data );
$data = str_replace("'", "''", $data );
return $data;
}

Open and attach file to mailer from database string [duplicate]

So:
// Setup mail class, recipients and body
$mailer->AddAttachment('/home/mywebsite/public_html/file.zip', 'file.zip');
The AddAttachment function has four arguments:
AddAttachment(PATH_TO_FILE, FILENAME, ENCODING, HEADER_TYPE)
I used to use xmail() and when I added a attachment here, I passed the filename and the content, that should be in it.
Like this:
$xmail->addAttachment('myamazingfile.pdf', $content);
How can I make it work the same way, so when i call AddAttachment() from the PHPmailer class, I can either pass the same or something like it, so I dont need to have a actual file on my server to send?
AddStringAttachment($string,$filename,$encoding,$type)
eg
$mail = new PHPMailer();
$mail->AddStringAttachment($string,$filename,$encoding,$type);
https://phpmailer.github.io/PHPMailer/classes/PHPMailer-PHPMailer-PHPMailer.html#method_addStringAttachment
since that AddAttachment() function is expecting a path rather than byte data, you should do a php convert to temp file function and then pass that path string into your function
$prefix = 'ConvertMediaArgs_'.time().'_';
$tempfile = tempnam( $this->tempdir, $prefix );
// Args file create failure: kill script with TEMPFILEFAIL error
if($tempfile === false) {
die('file could not be created');
}
// Write args as Key=Val (\n) to file
$fullpath = $this->tempdir.$tempfile;
$content = $someContent // <---------------- this is your file's data
$handle = fopen( $tempfile, "w");
fwrite( $handle, $content );
// $fullpath is the path you wanna pass to your function
$xmail->addAttachment( $fullpath, $content );

PHPMailer attachment, doing it without a physical file

So:
// Setup mail class, recipients and body
$mailer->AddAttachment('/home/mywebsite/public_html/file.zip', 'file.zip');
The AddAttachment function has four arguments:
AddAttachment(PATH_TO_FILE, FILENAME, ENCODING, HEADER_TYPE)
I used to use xmail() and when I added a attachment here, I passed the filename and the content, that should be in it.
Like this:
$xmail->addAttachment('myamazingfile.pdf', $content);
How can I make it work the same way, so when i call AddAttachment() from the PHPmailer class, I can either pass the same or something like it, so I dont need to have a actual file on my server to send?
AddStringAttachment($string,$filename,$encoding,$type)
eg
$mail = new PHPMailer();
$mail->AddStringAttachment($string,$filename,$encoding,$type);
https://phpmailer.github.io/PHPMailer/classes/PHPMailer-PHPMailer-PHPMailer.html#method_addStringAttachment
since that AddAttachment() function is expecting a path rather than byte data, you should do a php convert to temp file function and then pass that path string into your function
$prefix = 'ConvertMediaArgs_'.time().'_';
$tempfile = tempnam( $this->tempdir, $prefix );
// Args file create failure: kill script with TEMPFILEFAIL error
if($tempfile === false) {
die('file could not be created');
}
// Write args as Key=Val (\n) to file
$fullpath = $this->tempdir.$tempfile;
$content = $someContent // <---------------- this is your file's data
$handle = fopen( $tempfile, "w");
fwrite( $handle, $content );
// $fullpath is the path you wanna pass to your function
$xmail->addAttachment( $fullpath, $content );

PHP: how to write in between content of existing file?

How to write in between content of existing file?
I tried to use fseek() to seek a new position and write the new content, but it replaces old content to the new string after seek position.
My aim is to put new contents after 5 characters of existing file.
Old content: AAAAABBBBB, desired content: AAAAAnewcontentBBBBB
$file_handler = fopen('putty.log','w');
$new_content = 'this is new content';
fseek($file_handler,5);
echo ftell($file_handler); //5
fwrite($file_handler,$new_content);
old content replaced with NULLNULLNULLNULLNULLthis is new content
You can't do it that way.
You can only truncate the content with ftruncate and write later the old content
A not so clean example
<?php
$file_handler = fopen('putty.log','rb+');
$new_content = 'this is new content';
fseek($file_handler,5);
$restOfContent = fread($file_handler,filesize('putty.log')-5);
ftruncate($file_handler,5);
fseek($file_handler,5);
echo ftell($file_handler); //5
fwrite($file_handler,$new_content);
fwrite($file_handler,$restOfContent);
fclose($file_handler)
Load contents to variable using file_get_contents().
Do this on your buffer.
Save your contents to this file using file_put_contents();
if you're using fseek() to write data to a file, remember to open the file in "r+"
mode, example:
$fp=fopen($filename,"r+");
Writing in the middle of the file won't cause it to stretch and push the content forward (like when inserting content in a text editor) but rather to overwrite the content at the position you start writing.
What you need to do is:
read the first part of the old data until the point where you need to write the new content & write that part to a temporary file.
write the new content to the temporary file (the stuff you wish to add).
read the rest of the content from the old file & write it to the temporary file.
delete the old file.
rename the temporary file to the name of the old file.
Example:
$original_file_name = '/tmp/putty.log';
$temp_file_name = '/tmp/putty.tmp';
$temp_file = fopen( $temp_file_name, 'w' );
$file_handler = fopen( $original_file_name, 'r' );
$old_data_size = 5;
fwrite( $temp_file, fread( $file_handler, $old_data_size ) );
$new_content = 'this is new content';
fwrite( $temp_file, $new_content, strlen( $new_content ) );
fwrite( $temp_file, fread( $file_handler, filesize( $original_file_name ) - $old_data_size ) );
fclose( $file_handler );
fclose( $temp_file );
unlink( $original_file_name );
rename( $temp_file_name, $original_file_name );
Make sure that putty.log has read/write permissions for the user used by your webserver (apache/lighttpd etc.) process or that it's accessible for everyone (not recommended).

Categories