一、fpd转word图片丢失
在使用fpd转换成word文档再使用word转换成图片时,可能会出现转换后图片丢失的情况。这是因为word中转换的图片质量和尺寸默认设置较低。可以通过修改word中的默认设置解决。
// 设置word中图片的质量和尺寸
$word = new \COM("Word.Application");
$word->Optionen->EmbedTrueTypeFonts = false;
$word->Optionen->UpdateFieldsAtOpen = false;
$word->Visible = 0;
$word->Documents->Open(realpath("./template.docx"));
$word->Options->ViewAnimEffects = 0;
$word->Options->AllowReadingMode = 0;
$word->ActiveDocument->PageSetup->TopMargin = 25;
$word->ActiveDocument->PageSetup->BottomMargin = 25;
$word->ActiveDocument->PageSetup->LeftMargin = 25;
$word->ActiveDocument->PageSetup->RightMargin = 25;
$word->ActiveDocument->PageSetup->GutterPos = 0;
$word->ActiveDocument->PageSetup->HeaderDistance = 36;
$word->ActiveDocument->PageSetup->FooterDistance = 36;
$word->ActiveDocument->PageSetup->PageWidth = 595;
$word->ActiveDocument->PageSetup->PageHeight = 842;
$word->ActiveDocument->PageSetup->Orientation = 0;
$word->ActiveDocument->PageSetup->MirrorMargins = false;
$word->ActiveDocument->PageSetup->TwoPagesOnOne = false;
$word->ActiveDocument->PageSetup->BookFoldPrinting = false;
$word->Options->DefaultBorderColor = 1;
$word->Options->PrintBackgrounds = true;
$word->Options->AllowPNG = true;
$word->Options->DoNotPromptForConvert = false;
$word->ActiveDocument->SaveAs(realpath("./output.docx"));
$word->ActiveDocument->Close(false);
$word->Quit(false);
unset($word);
二、phppdf转png
在将phppdf文件转换成png图片时,需要注意的一些事项。
1、引入pdf库
确保引入了pdf库,并具备读取pdf文件和相关操作的权限。
require_once("pdf.php");
2、定义常量和实例化对象
常量定义了渲染的dpi和图片保存的格式。$pdf文件保存的路径需要根据具体情况进行替换。
define('DPI', 144);
define('IMAGE_FORMAT', 'png');
$pdf = new pdf();
$pdf->open("filepath.pdf");
3、设置页面尺寸和颜色空间
设置页面的尺寸和颜色空间,可以通过getPageWidth()和getPageHeight()方法获取页面尺寸,通过getColorSpace()方法获取颜色空间。
$pageWidth = $pdf->getPageWidth(); $pageHeight = $pdf->getPageHeight(); $colorSpace = $pdf->getColorSpace();
4、设置页面转换的宽度和高度
可以通过设置宽度和高度来控制图片的质量和尺寸。
$width = $pageWidth * DPI / 72; $height = $pageHeight * DPI / 72;
5、设置缩放和旋转参数
预览页面可能会包含缩放或旋转,可以通过setScale()和setRotate()方法进行控制。
$pdf->setScale($scale); $pdf->setRotate($rotate);
6、渲染页面并保存图片
通过调用getImageBlob()将渲染出来的图片数据保存到文件中。
$image = $pdf->getImageBlob($width, $height, $colorSpace, DPI);
file_put_contents("filepath.png", $image);
7、关闭pdf对象
关闭pdf对象,释放内存。
$pdf->close();
三、小结
在对phppdf转换成图片进行详细阐述后,我们深入理解了对于fpd转word图片丢失的原因以及解决方法。同时,我们对phppdf转png需要注意的事项进行了详细解释和演示代码,希望这篇文章能够对于您有所帮助。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/191142.html
微信扫一扫
支付宝扫一扫