Using the above technologies, I want to create a PDF, store it in my db, and email it. All with the click of one button.
I also want to call it up and have it be able to display with a hyperlink.
I am very new to FPDF. Therefore, I am trying to start off very slowly.
I began with this link stackoverflow Q
I put both parts of his code into the same page and tried with separate pages. I made the suggested changes/additions and even did a line by line comparison.
I still get the message, "format error: not a PDF or corrupted"
If I just $pdf->Output(); I get the pdf to display. It's either the way the string is being Output, or it's the header() function. It's not the storage method, unless my column setup is incorrect. BUt a blob is a blob, right?
If you want, I can upload the sanitized code. Just let me know what would help answer this.
Thanks
JJ
here's the code on request
here's where I enter it in:
<?php
session_start();
include "server.php";//my file to connect to db
require('fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$content = $pdf->Output("", "S"); //return the pdf file content as string
$sql = "update table set table_pdf= '".addslashes($content)."' " .
"where table_id = '188'";
mysql_query($sql);
//here's where I retrieve it
$sql2 = "select table_pdf from table where table_id = '188'";
$result2 = mysql_query($sql2);
$rs = mysql_fetch_assoc($result2);
$content2 = $rs['rdngs_hdr_pdf'];
header('Content-Type: application/pdf');
header("Content-Length: ".strlen(content2));
header('Content-Disposition: attachment; filename=myfile.pdf');
print $content2;
?>
Like I said, I have tried the other ideas on the other question link above. right now it just sits on the version where the addslashes is there.
Thanks for any help.
Give this a try. Instead of using the addslashes to escape the content, try using unpack to get it in a binary represenation:
$content = $pdf->Output("", "S"); //return the pdf file content as string
$data = unpack("H*hex", $content);
$sql = "update table set table_pdf= " . 0x".$data['hex']." . " " .
"where table_id = '188'";
For retrieving the data you should be able to just do a select, and then output the content, just like you are already doing.
Related
I'm very new to development, and I'm trying to retrieve an image from an SQL server over an odbc connection using the below:
<?php
require ('connect.inc.php');
$sql = "SELECt image FROM test.dbo.clients WHERE id = 1";
$sql_exec = odbc_exec($con, $sql);
if($sql_array = odbc_fetch_array($sql_exec)){
$image = base64_encode($sql_array['image']);
header('Content-type: image/jpeg');
echo "<img src=".$image."/>";
}
?>
The issue is that I'm getting an icon showing a broken image.
The query is correct as when I change it to the code below, it returns this string instead of the image:
Q2hyeXNhbnRoZW11bS5qcGc=
<?php
require ('connect.inc.php');
$sql = "SELECt image FROM test.dbo.clients WHERE id = 1";
$sql_exec = odbc_exec($con, $sql);
if($sql_array = odbc_fetch_array($sql_exec)){
$image = base64_encode($sql_array['image']);
echo $image;
}
?>
I know the code is not correct and might be vulenrable to SQL injection, however I'd appreciate if you can help me retrieve the image.
Many thanks in advance,
J
If you decode the base64 string you get, then you'll see that the decoded data is Chrysanthemum.jpg. This is just the filename of the image, not the image data.
You need to either store the image in the database (not the filename) or add some code to read the image from the filesystem.
BTW, Content-Type image/jpeg requires that the data is the raw image, but your content (<img src=...> ...</img>) is an HTML fragment.
Here is my code for downloading attachment from Salesforce.com using php toolkit and enterprise wsdl:
header('Content-Type: application/force-download');
header('Content-Disposition: inline; filename="image.jpg"');
$mySforceConnection = getConnection();
$query = "SELECT Id, Name, Body from Attachment Where Id ='" .$id ."'";
$queryResult = $mySforceConnection->query($query);
$records = $queryResult->records;
print_r(base64_decode($records[0]->fields->Body));
When I do this the file gets downloaded correctly with correct number of bytes but when I open the image, the windows image viewer says its corrupt. Any idea why this is happening?
The same code works fine for PDFs and text files.
You really want to just echo the output, as #eyescream mentioned. When you use the print_r function, additional tab and newline characters are placed into the output to make it more readable. A plain echo would output properly.
header('Content-Type: application/force-download');
header('Content-Disposition: inline; filename="image.jpg"');
$mySforceConnection = getConnection();
$query = "SELECT Id, Name, Body from Attachment Where Id ='" .$id ."'";
$queryResult = $mySforceConnection->query($query);
$records = $queryResult->records;
echo base64_decode($records[0]->fields->Body);
I have a small problem with the php content-disposition, I kind of understand where the problem lies but I have no idea how to solve it (new to using databases). Calling this php page will result in not showing any of the echos and only showing the download box, which I intended for the "cv" only (not sure if it's working that way, because the downloadable file I receive cannot be opened)
Removing the header(content... line will result in showing the echos, but I won't be able to download the specified file. I want it to show as a link which would download its contents when clicked.
$newEmployeeName = $_POST['name'];
$newEmployeeArea = $_POST['area'];
$newEmployeeCV = $_POST['cv'];
include('databaseConnection.php');
$result = mysql_query("SELECT * FROM participants");
while($row = mysql_fetch_array($result))
{
$download_me = $row['cv'];
header("Content-Disposition: attachment; filename=$download_me");
echo $row['name'] . " " . $row['area_of_exp'] . " " . $download_me;
echo "<br />";
}
The Content-Disposition header will force the script to present anything echoed after it as a download. You would normally use this with reading a file from the file system, so you can offer that as a download. In your case, if you’re storing CVs on your server then you may offer them as a download as follows:
<?php
$sql = "SELECT * FROM table WHERE id = :id LIMIT 1";
$stmt = $db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetchObject();
if ($row) {
header('Content-Disposition: attachment; filename=' . $row['filename']);
readfile($uploads_dir . $row['filename']);
exit;
}
else {
die('Invalid CV requested.');
}
Obviously the above is a simplified version of the process and you will need to tweak it to fit your application, but that’s the gist of it.
Also, don’t use the mysql_ functions. They’re deprecated (as per the warning on this page). Use either PDO or the new MySQLi (MySQL improved) extension.
I want to insert a image in blob format to db.
how to write a program in zend framework.
a simple form
just choose one image and insert into db.
In your Zend form create a file element:
$element = new Zend_Form_Element_File('fileElement');
Then you can insert the uploaded image to your DB in BLOB format like this:
$conn = new PDO("mysql:host='host';dbname='database'", 'userName', 'password');
$imagePath = $zendForm->fileElement->getFileName();
$image = file_get_contents('$imagePath');
$sql = "INSERT INTO images (data) values(?)";
$q = $conn->prepare($sql);
$q->bindParam(1, $image, PDO::PARAM_LOB);
$q->execute();
Read it in as a string using file_get_contents and store that.
That said, it is seldom a good idea to store the actual image data in the database. It is better practice to generate a unique filename and store that in the DB instead.
Here is some code to save the file in zend:
// This is to save in saveAction()
$source = $this->view->form->upload->getFileName();
$fp = fopen($source,'r');
$content = fread($fp,filesize($source));
// I don't use addslashes, because i already have that in my mapper
fclose($fp);
$model->image = base64_encode($content);
// Save here
// This is to read in imagesAction()
$this->_helper->Layout()->disableLayout();
$this->_helper->ViewRenderer->setNeverRender();
header('Content-Type: image/gif');
echo base64_decode($image);
exit;
I had alot of problems with it, try to make your save work and don't insert it into the database directly to test the reading. This won't make it possible unless you know the correct db encoding (which i don't).
Hi I have a image table in my database. These are stored as blob along with details such as image type & name.
I am having a problem showing the image, all I get is a white box with a red cross in it.
code:
<?php
include '../connection.php';
$ID = $_GET['id'];
$query = "SELECT * FROM `images` WHERE `image_id` = '$ID'";
$result=mysql_query($query);
$row = mysql_fetch_array($result);
$image = $row['image'];
$image_type= $row['image_type'];
header("Content-type: $image_type");
print $image;
exit;
?>
Thanks
Well here is a short answer.
<?php
include '../connection.php';
$id = (int)$_GET['id'];
$query = "SELECT * FROM `images` WHERE `image_id` = '$id'";
$result=mysql_query($query);
$row = mysql_fetch_array($result);
$image = $row['image'];
$image_type= $row['image_type'];
$size = $row['image_size'];
//alternative
/* list($image, $image_type, $size) = array(
$row['image'],
$row['image_type'],
$row['image_size']
);
*/
$ext = explode('/', $image_type);
$name = $id . '.' . $ext[1];
header("Content-type: $image_type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
print $image;
exit;
Check your blobtype to be a least MEDIUMBLOB which is able to store data up to 16M
To debug this, I'd suggest commenting-out the Content-type header line, then hitting the url directly in the browser. This will allow you to see any errors, warnings or notices that PHP might be emitting.
Couple of things to try
maybe something is failing and it is returning an error msg in html instead of the expected image
is the content_type stored correctly in the database or are you just storing the file extension of the image
content-type should look something like this
image/gif
image/jpeg
That looks like it might work, what's going wrong?
Try to specify the fields explicitly:
SELECT image, image_type FROM ...
What happens when you run the query from the database?
Are you loading the image like:
<img src="image.php?id=12">
Or do you load the PHP as its own page?
maybe you could try
$row = mysql_fetch_assoc($result);
instead of
$row = mysql_fetch_array($result);
You said in a comment that the table initially said "[BLOB - 64.0 KiB]" but you then changed it to "MEDIUMBLOB". This will expand the size that you can store, but all of your existing data will still be truncated to 64KiB.
Make sure that the field type you use is large enough to store the data you want to store (16mb in a MEDIUMBLOB or ~4gb in a LONGBLOB I'm pretty sure) and then re-insert all of your data.
Other than the security problems mentioned, I don't see why the code shouldn't work other than the database problem.
Or if you don't want to create a separate php file, you can inline it
<?php
// retrieve blob into $img
?><img src='data:image/png;base64,<?php echo base64_encode( $img );?>' alt='Image <?php echo $id;?>'>