i'm trying to use fdpf with php to generate a pdf file with patient automatic reports system
so after getting all patient info from DB .. i'm trying to print report image from directory
$id=$user_id;
$dir="./user_reports/$id";
opendir("$dir");
$fi = new FilesystemIterator($dir, FilesystemIterator::SKIP_DOTS);
$report_img=array();
for($k=1;$k<=iterator_count($fi);$k++) // iterator_count to count files in folder
{
$report_img[$k]="$dir/$id.jpg";
$id++;
}
and this is to print info.
for($x=1;$x<=$i;$x++)
{
$image=$report_img[$x];
$pdf->Cell( 40, 40, $pdf->Image($image,70,190,-300), 0, 0, 'L', false );
}
after all .. it doesn't work :(
any ideas?
You are assuming $pdf->Image($image,70,190,-300) returns a string which you can put in a cell. This is not so. Try this:
for($x=1;$x<=$i;$x++)
{
$image=$report_img[$x];
$pdf->Image($image,70,190,300);
}
I left out the cell, because it doesn't do anything. I used a positive width for the image.
I'm sure this doesn't do what you want. That's unclear at this point, but it shouldn't generate an error 500 anymore, as long as the images exist.
Generating a PDF file like this is nothing like working with HTML. You're probably thinking of HTML table cells.
In the case you want to print something, you can work with just HTML. It is now possible to make pages in HTML and set the true dimensions when printing with styles. See: https://www.w3.org/TR/css3-page/#page-size
That could be a much better solution for you, since you seem to know HTML. It wouldn't work if it is actually the PDF file you want.
Related
I have a site which displays a dynamically generated GD image (data pulled from SQL). I need to view this image to pinpoint inconsistencies in my coding. These aren't errors, but rather just simple variables that are out of place that I must isolate. The easiest way I've found to do this is to simply echo the variables at different points in the code.
The problem is that the GD Image will overlay the page and so nothing is displayed from echo/print. The only way I can call the variables/functions the way I want to is by clicking on unique spots on this image. So the image must be there to echo the correct php variables. Both must be present at the same time. The only way I can echo/print is to turn off the image, but then these variables tell me nothing without being able to look at the image.
I don't want to use breakpoints/debugging etc as I'm on a shared host and don't have access to the extensions required for this, and I don't want to run a server locally for development.
I'm pulling the image in an img src="image.php" tag, and the PHP is stored in an external file called functions.php. The index.html which brings it all together is just jquery/javascript.
How can I echo the outputs with the GD image still displayed?
Output your debugging to the log.
error_log('Your message here...');
http://php.net/manual/en/function.error-log.php
You could buffer the output of the script using ob_start()/ob_get_contents() and then use imagestring to draw those logs on the image like so:
ob_start();
echo("Foo");
$out = ob_get_contents();
imagestring($image, 5, 0, 0, $out, imagecolorallocate($image, 255, 255, 255));
ob_end_clean();
Hi I have searched the web for 2 days but did not accomplish what I am looking for.
I have an apache server which will be accessed by 146 students. the user picks an angle from dropdown lets say 45 degress, then user clicks CALCULATE button. Then user clicks DIAGRAM button to see how the sine graph looks like.
Works like charm when i write the image to a file e.g: imagepng($img,"diagram.png");
Now the problem is that the diagram.png will always get overwritten by the last user. So for example if another user logs in and calculates the Sin 135. Both users will see Sine 135 because filename is hardcoded since there is conflict of filename.
I have searched the web on how to create the image dynamically instead of writing to a file and then reading the file. I have come across the following but not working:
base64_encode and decode
What would I have to do to my code of imagepng(...., ...) mentioned above to make use of base64 so I can actually draw the picture of already processed data. Let assume if I comment out the imagepng(..) code, then what do I replace it with. I hope I don't have to change my code a whole lot.
Please help
thanks
Amit
The filename argument to imagepng is optional. From the manual:
filename
The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.
You would just need to send a png header at the top of the script and you would get the image as output for that script.
It's hard to tell without seeing you code how it is structured
but if once the user submits the form all you do is show the image by itself, then you can do something like this.
// make sure nothing else is out put before this otherwise it will stuff up the header
header('Content-Type: image/png);
imagepng($img);
If you embed the image into an html page as the result, then your best best would be to change the url of the image on the success page to something like this.
<img src="/path/to/file.php?deg=45" />
Then in the file.php
$deg = $_GET['deg'] + 0; // make sure it is a number
$img= function_render_graph($deg);
// make sure nothing else is out put before this otherwise it will stuff up the header
header('Content-Type: image/png);
imagepng($img);
By using a GET request, rather then a POST request then the image will likely be cached by the browser, so it doesn't need to be rendered each time. (Given that you have a drop list of angles, there must be a limited number of graphs that can actually be drawn)
Draw_Resultant_Prism_Graph (parameters)
{
$img = imagecreatetruecolor(800,750);
....
....
...
the following lines captures the data from output buffer and displays on same screen
***some version of IE have some issues mostly the dumb terminals where IE update is ADMIN
***restricted
ob_start();
header("Content-type: image/jpeg");
imagepng($img);
$output = ob_get_contents();
ob_end_clean();
imagedestroy($img);
echo img src="data:image/jpeg;base64,'.base64_encode($output).'"
user tags around img above and semicolon af
}
I have a pdf doc which has 2 pages per page I want to split the page vertically and display the page in order (eg 100 page pfd doc will get converted to 200 pages doc)
I want to code it in may be php/javascript (is it possible). I want to upload any pdf doc to my website and then split it vertically then display the pages in order
Please let me how how to do this, I have tried to google but I could not find a solution
thanks
Edit:
I am trying to use clipRectangel() method:
<?php
require_once 'Zend/Pdf.php';
$pdf = Zend_Pdf::load('pages.pdf');
$page = $pdf->pages[0];
$page->clipRectangle(10, 10, 30, 30);
$pdf->save('new.pdf');
?>
I am trying to use clipRectangle to split vertically but I am not getting any modified pdf in 'new.pdf' I also want to order the pages,please suggest any better way to use zend. It would be helpful if you can give me few lines of code.
This is a pretty tall order.
Your best bet is to use Zend_Pdf which allows loading and altering of (unprotected) PDF files in php. Javascript cannot do this.
This is of course not something that will work with just any PDF in any circumstance. You can get cut off text (half on one page, half on the other) as the entire process is not like HTML text.
Converting a PDF to HTML is a whole other thing.
Update: I've taken your code simply for illustration. Haven't tested this, but if you get errors, you should be able to find the solution rather quickly.
require_once 'Zend/Pdf.php';
$pdf = Zend_Pdf::load('pages.pdf');
$pdfnew = new Zend_Pdf();
foreach ($pdf->pages as $page) {
$curpage = $page;
// Clipping for first part, enter your own numbers..
$curpage->clipRectangle(10, 10, 30, 30);
$pdfnew->pages[] = $curpage;
$curpage = $page;
// Clipping for second part, enter your own numbers..
$curpage->clipRectangle(10, 10, 30, 30);
$pdfnew->pages[] = $curpage;
}
$pdfnew->save('new.pdf');
I wrote a simple image randomizer on PHP that picks a random image from a list using the rand() function. The code works perfectly, and a random image is generated when I include it on my html as a picture.
The problem comes when I try to include it twice in the same html. A random image WILL be generated and displayed for both times I included it, but it will be the same image. In other words, I get a repeated random image on my page.
An easy way to solve this is to simply copy the randomizer.php, give it a new name, and include both images in HTML. The reason I don't want to do this is because my final HTML will have about 25 pictures, and I simply feel like there should be a better way to do this. Keep in mind that I CANNOT add any PHP functions into my HTML, given that my files are hosted in different servers, and my HTML server does not support PHP.
If anyone know of a better fix other than creating 25 copies of my randomizer.php file (or creating 25 different files that include it), please let me know. I will most definitely appreciate your input!!
Thank you very, very much!!
Here's a snippet of the code:
if (count($fileList) > 0) {
do { //do-while loop will get a new random image until that image has not been used yet in this session
$imageNumber = rand( 0 , ( count($fileList) - 1) ); //get random image from fileList
$iterations++;
} while( !(empty($_SESSION['img' . $imageNumber])) && iterations < 200);
$_SESSION['img' . $imageNumber] = True; //this image number has been displayed
$_SESSION['shown']++; //increments the number of shown pictures in this signature
$img = $folder.$fileList[$imageNumber];
}
It may be that the browser thinks it is the same image and is caching, try setting the name of the image (emit a header with content-disposition/filename IIRC) and/or adding a unique tag to the end of the image name with a random string, ( e.g. image.jpg?e0.6613725793930488 )
My guess is that rand() either didn't reseed, or is seeded with the same value.
Have you considered calling srand() - or "the better random number generator" combination of mt_srand() and mt_rand()?
I'm currently writing a code to output a pdf file in PHP using PDFlib from http://www.pdflib.com/. The problem is all html tag is also written in the output file. How can be able to cancel out all those tags?
Here is my sample code.
$postVariable = $_POST;
$contentData = "";
foreach($postVariable as $key => $value){
if(is_array($key)){
foreach($key as $key1 => $value1){
$contentData.= $key1 .": ". $value1."<nextline>";
}
}else{
$contentData.= $key .": ". $value."<nextline>";
}
}
$testdata = nl2br($contentData);
$pdf = pdf_new();
// open a file
pdf_open_file($pdf, $_SERVER['DOCUMENT_ROOT']."cas".DIRECTORY_SEPARATOR."$filename.pdf");
// start a new page (A4)
pdf_begin_page($pdf, 595, 842);
// Locate Font Directory
$fontdir = "C:\WINDOWS\Fonts";
// Font Name Parameters
pdf_set_parameter($pdf, "FontOutline", "arialMyName=$fontdir\arial.ttf");
// Find Font
$arial = PDF_findfont($pdf,"arialMyName","host",0 );
// Set font size and font name
pdf_setfont($pdf, $arial, 10);
//$arial = pdf_findfont($pdf, "Arial", "host", 1);
//pdf_setfont($pdf, $arial, 10);
// print text
pdf_show_xy($pdf, "TO THE UNIT OWNER",50, 750);
pdf_show_xy($pdf, "Test ext", 50,730);
pdf_show_xy($pdf, "test test", 50,715);
pdf_show_xy($pdf, $contentData, 50,700);
// end page
pdf_end_page($pdf);
// close and save file
pdf_close($pdf);
and the sample output is:
TO THE UNIT OWNER
Test text
test test
type: Apartment****var_name: ****var_company: ****var_date: ****submit: Save and Download<
It disregards the html tags and it include it on the content.
Is there any other methods on how to print out HTML to PDF using the library that I'm currently using (PDFlib).
Thanks.
regards,
Resty
I would recommend the TCPDF library. It can convert your HTML to a PDF file, including CSS code. It has support for quite a lot of HTML tags, and does a decent job. HOWEVER, from personal experience, while it is very possible to generate high quality PDF files with it, the results are not always 100% as expected. It might need a bit of tweaking and fiddling to get the result you want.
If you only want to remove the HTML tags, I think you might want to take a look at strip_tags. But I guess that's not really what you're after.
Finally, there's a really really cool new kid on the block - a PHP extension that uses the WebKit HTML and rendering engine to generate PDFs. It is called the libwkhtmltox extension and can be found here. It produces amazing results. For example, look at this PDF it made from the homepage of the New York Times - see (http://www.2shared.com/document/kYuS_G7p/nytimes.html - click "save to my pc" down below). That output was generated without specifying any additional options. Exceptional. HOWEVER, you need to get it running as PHP extension, and that might be a non-trivial task. So I would say: stick with the TCPDF library for now.
You cannot generate PDF content from HTML by using PDFLib. Ok, you can write convertor, but that's a huge work to do.
I recommend using TCPDF which supports HTML input.
Man... I was trying to do same thing once. It is very hard to work with pdflib directly.
I found this project, hosted on Google code site. The project deserved 10 stars out of 5.
It is called DOM PDF, you can download it here. You can generate PDFs from HTML pages, with CSS support. You can generate PDF with less than 10 lines of code!
Look at samples to see it in action.