FPDF error: Not a JPEG file: http://10.11.201.93:81/webdocc/uploaded/tes3.jpg

FPDF error: Not a JPEG file: http://10.11.201.93:81/webdocc/uploaded/tes3.jpg

我正在使用 fpdf 将 html 转换为 pdf 。我在 new.html 中有以下 html 。

<title></title>
<p><img alt="" height="364" src="http://10.11.201.93:81/webdocc/uploaded/tes3.jpg" width="496" /><img alt="" height="470" src="http://10.11.201.93:81/webdocc/uploaded/tes4.jpg" width="641" /></p>

将html转换为pdf的代码如下:

<?php
require('html2fpdf.php');
$pdf=new HTML2FPDF();
$pdf->AddPage();
$fp = fopen("new.html","r");
$strContent = fread($fp, filesize("new.html"));
fclose($fp);
$pdf->WriteHTML($strContent);
$pdf->Output("new.pdf");
echo "PDF file is generated successfully!";
?>

但是当我 运行 此代码时,出现以下错误。

FPDF error: Not a JPEG file: http://10.11.201.93:81/webdocc/uploaded/tes3.jpg

在以下 html 代码中,出现错误 "Alpha channel not supported"。

<h1 id="sample_title"><img alt="" src="http://10.11.201.84/document-editor/uploaded/applet.PNG" style="width: 514px; height: 204px;" /></h1>

我怎样才能消除这个错误?请帮我 。

对于"Not a JPEG file": 最好的办法是重新导出图像文件。只需在 Gimp、Photo Shop 等中打开它,然后重新导出为 jpeg。每次我遇到这个问题时,我都用 Gimp 重新导出,它修复了 FPDF 认为是图像的非 jpeg 部分的任何内容。

对于"Alpha channel not supported": 这是因为 FPDF 不支持 Alpha 通道。我相信它确实支持索引透明度,因此您可以在关闭 alpha 通道并打开索引透明度的情况下重新保存图像(同样是 Gimp、Photo Shop 等)。

您可能还想查看 DomPDF 它是一个支持 Alpha 通道的 HTML TO PDF 转换器。如果您生成的是大型 PDF(许多页面、图像等),您可能需要增加执行时间。

您还可以查看的另一件事是,粉丝在 FPDF 中支持 Alpha Alpha Channels / Masks

我也遇到过同样的问题很多次,然后我开始阅读 FPDF 的所有代码。 我通过更改某些行 fpdf.php 得到了我的解决方案。 FPDF.phpFPDF 库中的核心文件。所以在任何更改之前保留此文件的备份。

我刚刚在这个文件中搜索过

Not a JPEG file

然后我注释掉这一行并设置一个默认值

//$this->Error('Not a JPEG file: '.$file);

$colspace ="DeviceRGB";

在此之后,我的 PDF 将生成。 请注意,在我的情况下,图像路径是有效的,并且我使用 base64 解码 CropingS3 上上传图像] 也。所以也许我的形象出了问题。但它正在浏览器上打开。所以在fpdf.php上进行两次更改后。我得到了我的解决方案。

您必须检查的另一件事是图像路径是否有效。在我正在使用的 pdf 文件中

$pdf->Image($file,12,12,30,30);

发现多年来在 MacBook Pro 上使用 Paintbrush 制作幻灯片有时会保存 "file jpeg_filename.jpg" 确定为 PNG 的 JPEG,这对浏览器而言并不是世界末日去渲染这个。在 FPDF 的 fpdf.php 中,我通过下面的笨拙 "if($a[2]==3) { return $this->_parsepng($file); }" 附加代码行修复了导致 "FPDF Error: Not a JPEG file" 的自己的缺点 ...

function _parsejpg($file)
{
  // Extract info from a JPEG file
  $a = getimagesize($file);
  if(!$a)
    $this->Error('Missing or incorrect image file: '.$file);
  if($a[2]==3) { return $this->_parsepng($file); }
  if($a[2]!=2)
    $this->Error('Not a JPEG file: '.' '.$a[2].' '.$file);
  if(!isset($a['channels']) || $a['channels']==3)
    $colspace = 'DeviceRGB';
  elseif($a['channels']==4)
    $colspace = 'DeviceCMYK';
  else
    $colspace = 'DeviceGray';
  $bpc = isset($a['bits']) ? $a['bits'] : 8;
  $data = file_get_contents($file);
  return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$data);
}