php通过fpdf将生成的png包含在pdf中 [英] php include generated png in pdf via fpdf

查看:152
本文介绍了php通过fpdf将生成的png包含在pdf中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用同样由php生成的图像来生成PDF. 听起来很简单,我敢肯定我只是弄糟表头,但似乎无法在此处找到解决方案.

I am trying to generate a PDF with an image that is also generated by php. Sounds simple enough and I am sure I'm just screwing up the header but I can't seem to find a solution here.

首先我生成一个PDF:

first I generate a PDF:

define('FPDF_FONTPATH','fonts/');
require('scpt/fpdf.php');

class PDF extends FPDF {}
$pdf = new FPDF('P','in',array(8.5,11));
$pdf->SetAutoPageBreak(false,0);

$pdf->SetTextColor(0,0,0);
$pdf->SetDrawColor(0,0,0);
$pdf->SetFont('Helvetica','',12);

$pdf->Image('label.php?imgid=17',0,0,0,0,'PNG');

$pdf->Output('label.pdf','D');

然后我在label.php中生成PNG:

then I generate the PNG in label.php:

if(isset($_GET["imgid"])) {
 header("Content-Type: image/png");
 $im = @imagecreate(110, 20)
     or die("Cannot Initialize new GD image stream");
 $background_color = imagecolorallocate($im, 0, 0, 0);
 $text_color = imagecolorallocate($im, 233, 14, 91);
 imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);
 imagepng($im);
 imagedestroy($im);
}

这将输出:FPDF error: Not a PNG file:....

在浏览器中调用label.php?imgid = 17,但是会向我显示图像...

calling label.php?imgid=17 in the browser however will show me the image just fine...

我想念什么?

编辑

在文档中: 例子

// Insert a logo in the top-left corner at 300 dpi
$pdf->Image('logo.png',10,10,-300);
// Insert a dynamic image from a URL
$pdf->Image('http://chart.googleapis.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World',60,30,90,0,'PNG');

因此应该可以包含动态生成的图像而不先保存它吗?!

so it SHOULD be possible to include a dynamically generated image without saving it first?!

EDIT#2

我使它可以与库 mem_image 一起使用,但问题仍然在于不应该抛出一个错误的IMO ?!因此,我将这个问题悬而未决,以查看我的脚本中是否确实存在错误,而事实证明这是一个错误.

I made it work with the library mem_image but the problem remains that this should not throw an error IMO?! So I leave this question open to see if there is indeed something wrong with my script of this turning out to be a bug.

推荐答案

您几乎拥有了它,只缺少了几件事.首先,您需要在URL中用于Image()调用的完整服务器地址,因此,不是

You almost had it, you're missing only a couple things. First you need the full server address in the URL for your Image() call, so instead of

$pdf->Image('label.php?imgid=17',0,0,0,0,'PNG');

您需要:

$pdf->Image('http://www.yourserver.com/label.php?imgid=17',0,0,0,0,'PNG');

这将消除您遇到的FPDF错误.然后,要使FPDF正确呈现输出,您需要添加对AddPage()的调用,这样您的PDF生成脚本将变为:

That will eliminate the FPDF error you were encountering. Then to get FPDF to render your output correctly, you need to add a call to AddPage(), so your PDF generation script would become:

require('scpt/fpdf.php');

$pdf = new FPDF('P','in',array(8.5,11));
$pdf->AddPage();
$pdf->Image('http://www.yourserver.com/label.php?imgid=17',0,0,0,0,'PNG');

$pdf->Output('label.pdf','D');

当然,如果仅包含单个图像,则不需要SetTextColor(),SetFont()等.您也不需要自定义类定义(我删除了不需要的行).

Of course you don't need the SetTextColor(), SetFont(), etc if you're only including the single image. You didn't need the custom class definition either (I removed the unneeded lines).

别忘了用www.yourserver.com替代脚本的适当域和路径.

Don't forget to substitute www.yourserver.com for the appropriate domain and path for your script.

这篇关于php通过fpdf将生成的png包含在pdf中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆