FPDF / How to make display correctly - php

I'm using the fpdf library to output a pdf from html. The .pdf is being created because I can email it to myself and it arrives in the correct format but if I want to download the .pdf as an option without emailing, the output is illegible. The output appears in a browser window(see attached screen shot) and I'm unsure how to fix this issue.
I've also attached a screenshot of how we have our report options set up. 1. HTML 2. PDF 3. Download 4. Email -- the HTML and Email options work, the PDF and Download options do not. I'm focusing on the Download option in this question.
This is the output code that I've tried to test out but no luck
//$pdf->Output("D","D:/example2.pdf");
//$content = $pdf->Output("","S");
//$pdf->Output(); //Outputs on browser screen
//Outputs on browser screen
$pdf->Output();
//echo file_get_contents($pdf);
//readfile($pdf);
the $pdf->Output(); is generating the illegible code the readfile
echo and file_get_contents throw errors
the $pdf->Output(... gives me an error that says Incorrect Output
Destination (see attached screenshot)
Need guidance -- thanks for any help.
here is the full code:
<?php
$m_header = '<link href="shared/report.css" rel=stylesheet type="text/css">';
$m_body_tag = ' scroll=no';
require_once($DOCUMENT_ROOT."inc/top-2.inc.php");
$i_get_sid = isset($_GET["sid"]) ? (int)$_GET["sid"] : $i_sid;
$i_get_pass = isset($_GET["a"]) ? $_GET["a"] : $_SESSION['r_pass'];
$i_get_pass = addslashes($i_get_pass);
$i_pdf_file_url = 'report.php?sid='.$i_get_sid.'&a='.urlencode($i_get_pass).'&b=/report.pdf';
echo '<table cellpadding=0 cellspacing=0 border=0 width="100%">';
echo '<tr vAlign=top><td height=7><img src="images/1x1.gif" width=1 height=7></td></tr>';
echo '<tr height=28 style="background: url(images/bookm-bg.gif) repeat-x"><td width="100%"><nobr>';
echo '<img src="images/1x1.gif" width=5 height=1><img src="images/bookm-42.gif" width=67 height=28 border=0><img src="images/bookm-s1.gif" width=10 height=28 border=0><img src="images/bookm-51.gif" width=65 height=28 border=0> <img src="images/button-downloadpdf.gif" width=80 height=28 border=0> <img src="images/button-emailpdf.gif" width=80 height=28 border=0>';
echo '</nobr></td><td><nobr><font style="font-size: 10px;">Close Window </font></nobr></td></tr></table>';
(This is what I added as a workaround)echo '<p><a href="'.$i_pdf_file_url.'" name="plugin" width=100% height=100% fullscreen=yes style="position: absolute;">Click Here to open the PDF</p>';
(This is what should display the PDF in the browser but wont' work)
echo '<p><embed type="application/pdf" src="'.$i_pdf_file_url.'" name="plugin" width=100% height=100% fullscreen=yes style="position: absolute;"></p>';
require_once($DOCUMENT_ROOT."inc/btm-2.inc.php");
?>

From documentation:
Destination where to send the document. It can be one of the following:
I: send the file inline to the browser. The PDF viewer is used if available.
D: send to the browser and force a file download with the name given by name.
F: save to a local file with the name given by name (may include a path).
S: return the document as a string.
The default value is I.
If you want to force download with given name, create a link that points to your pdf script and use this:
$pdf->Output("__name__","F");
If you want to display pdf for preview use this:
$pdf->Output("__name__","I");
and in your html inside PDF tab use iframe to embed pdf:
<iframe src="pdf_preview.php" frameborder="0"></iframe>
There are other ways to do this, but this should be the easest.

Related

sprint to html file placeholder

i would like to pass a value to external html file using sprintf. What i have tried so far:
external html file:
<html><body><table border="0" cellpadding="0" cellspacing="0" style="width:100%%;padding:25px 20px 25px 20px;background:#f2f2f2;"><tr><td>%s</td></tr></table></body></html>
php file:
$html = "../../email-templates/registration-template.html";
$html = sprintf($html,"myvalue");
$mime->setHTMLBody($html, true);
i have escaped the % used for the css property but i keep getting %s and not "myvalue"
aparently it was not a big deal; i found a solution ...i get the html content of the external file using file_get_contents() method and in setHTMLBody() i passed the content as text and not as file
$html = sprintf(file_get_contents("../../email-templates/registration-template.html"), "myvalue");
$mime->setHTMLBody($html);

Php pdf image preview code

I try to generate php code in order to preview an image from a pdf file. Here is my code. There is an error but i cannot understand where it is. I would appreciate any help. Thank you.
<?php
//Get Menu Bar
include('navigate.php');
?>
<table cellspacing="4" width=100%><tr><?php if(!$vol){ ?><td bgcolor="eeeeee" width="50%" valign="top">
<h3 style=" text-shadow:#003">Latest News:</h3>
<?php
$latest=mysql_query("select * from articles ORDER BY id DESC LIMIT 1", $link);
while ($article = mysql_fetch_array($latest)) {
$thevolume= $article['volume'];
$title= $article['title'];
$author= $article['author_main'];
$abstract= $article['abstract'];
$pdf= $article['pdf'];
$im = new imagick('pdf');
$im->setImageFormat('jpg');
?>
<a href="<?php echo $pdf; ?> "PDF: ?php echo "$title - $author"; ?>
<img src="<?php echo $im; ?> " WIDTH="98%" border="1" title="<?php echo "$title - $author"; ?> " caption="Click here to open PDF" />
</a>
You must to pass as parameter a path to PDF file, not only the string 'pdf': http://php.net/imagick.construct
If $article['pdf'] contains it you must to use:
$im = new imagick(realpath($pdf) . '[0]');
$im still being a resource that contains a object instance of imagick, and not the image or pdf document itself, so you must to do this to inline image in HTML (if you want to use JPEG, I would use PNG):
<img src="data:image/jpg;base64,<?php
echo base64_encode($im->getImageBlob());
?> " WIDTH="98%" border="1" title="<?php
echo htmlspecialchars("$title - $author");
?> " caption="Click here to open PDF" />
Note the usage of htmlspecialchars ( http://php.net/htmlspecialchars ) to output strings to navigator and getImageBlob ( http://php.net/imagick.getimageblob ) to get image data (if available) and finally I will encode it in base64 to inline it in HTML document.
If you want to link to a cached version of image you must to save it in a temporary file or create another PHP script to generate the image from PDF file.
Best regards,
Edit 1: How to create a JPEG thumbnail, from PHP manual:
You should read this: http://php.net/imagick.setimageformat#89210
This example will help you:
// convert to JPEG
$im->setImageColorspace(255);
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(60);
$im->setImageFormat('jpeg');
Probably you will wish to reduce image size:
$im->resizeImage(290, 375, imagick::FILTER_LANCZOS, 1);
And remember to import the page number (beginning with 0, not 1) adding '[0]' to file name as edited.
Good luck!

to echo a <img > tag and send the code through email+ php

I have a php variable which contain the whole source code of image.What I want is to echo the tag with its attributes src,height & width and send the source code of image through mail (i.e ).
$imagename = abc.jpg;
$concatpath = SITE_URL."/uploads/affiliatesAdv/".$imagename;
$imagesrc = '<img src="'.$concatpath.'" height="200px" width="200px">';
Now when I echo $imagesrc it displays the image. How can I show the source code instead?
Try this,
echo "<pre>";
echo $imagesrc;
echo "</pre>";
You need to use html encoding.
Some thing like this
<img src="abc.jpg"/>
to
<img src="abc.jpg"/>
See the reference.
You can try this site to do it online..

png image mime type error cakephp

I have a plugin in a cakephp application located at app/plugins/my_plugin in which I have a webroot folder which contains some images in the app/plugins/my_plugin/webroot/img/ folder. I'm trying to put an image as the background of a span in one of the views of my_plugin. My span is for example:
<span class="my_span"><p>Content...</p></span>
And the css I use to but the image as background is:
.my_span
{
background-image:url('<?php echo $this->Html->image('/my_plugin/img/my_image.png', array('alt' => 'My Image'))?>');
}
When I do this, I get the following error:
Resource interpreted as Image but transferred with MIME type text/html
But the weird thing is, when I just try to put the image within my span tags (without putting it as a background through CSS) like:
<span class="my_span"><p><?php echo $this->Html->image('/my_plugin/img/my_image.png', array('alt' => 'My Image'))?></p></span>
My image displays just fine and I don't have any errors.
Can anybody tell me why I can't put the image as background of my span?
NOTE: I already tried display:block; or everything else for my span
The issue is that this code:
<?php echo $this->Html->image('/my_plugin/img/my_image.png', array('alt' => 'My Image'))?>
produces this
<img src="/my_plugin/img/my_image.png" alt="My Image" />
In your case you want to use this code:
.my_span
{
background-image:url(/my_plugin/img/my_image.png);
}
Reference: here

PDF view link in PHP

I am creating a web application where users can upload/download/view online pdfs. I want to change the name of the pdf file to view in new tab link (like we see in all websites).
Does anyone know how to make any field in the database that gives a link that when user clicks, it opens the pdf in a new tab?
if($result) {
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {
// Print the top of a table
echo '<table width="100%">
<tr>
<td><b>Name</b></td>
<td><b>type</b></td>
<td><b>Size (bytes)</b></td>
<td><b>Created</b></td>
<td><b> </b></td>
<td><b>view><b/></td>
</tr>';
// Print each file
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>{$row['name']}</td>
<td>{$row['type']}</td>
<td>{$row['size']}</td>
<td>{$row['created']}</td>
</tr>";
}
// Close table
echo '</table>';
}
// Free the result
$result->free();
}
else
{
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
}
// Close the mysql connection
$dbLink->close();
The solution doesn't have to be within the database.
You can use an anchor tag to redirect the user to the link :
<a href="{$path_to_pdf}" target="_blank" >Click here to view the PDF</a>
Put that in a cell in the table you are generating.
I could be mistaken - but I do believe that the users browser is also a factor here. The browser has to be able to display pdf's. The latest and greatest* browsers have it baked in but user preferences' might also be a factor.
* Let your imagination run wild
You can use target="_blank" in your a tag
PDF
This works in Google Chrome, not sure if it works in other browsers.
You can look here http://www.allaccessliving.com/residences/floorplans#1bed15bath:35A
There's a pdf link and you see for yourself if you inspect that element.
When outputting the link to the PDF, use this:
Visit StackOverflow
setting target will tell the browser to open the link in a new tab.

Categories