Php Zend_PDF:从PDF文档中删除html内容
我已经在自定义控制器中创建了函数getPdfPhp Zend_PDF:从PDF文档中删除html内容,php,zend-framework,magento-1.7,magento-1.6,zend-pdf,Php,Zend Framework,Magento 1.7,Magento 1.6,Zend Pdf,我已经在自定义控制器中创建了函数getPdf public function getPdf() { $pdf = new Zend_Pdf(); $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4); $imagePath="C:\Users\Hp\Desktop\landscape3.jpg"; $image = Zend_Pdf_Image::imageWithPath($imagePath); $page->drawImag
public function getPdf()
{
$pdf = new Zend_Pdf();
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$imagePath="C:\Users\Hp\Desktop\landscape3.jpg";
$image = Zend_Pdf_Image::imageWithPath($imagePath);
$page->drawImage($image, 40,764,240, 820);
$pdf->pages[] = $page;
$pdf->save('myfile.pdf');
}
它生成带有图像但位于magento1.7文件夹中的PDF。我试过以下几行
$pdfString = $pdf->render();
header("Content-Disposition: attachment; filename=mydownloads.pdf");
header("Content-type: application/x-pdf");
echo $pdfString;
它在下载文件夹中生成PDF文档,但当我打开它时,会显示一条错误消息:Adobe reader无法打开myfile.PDF,因为它不是受支持的文件类型,或者因为文件已损坏。。。。。。。。。。。。当我在文本编辑器(记事本)中打开mydownloads.pdf时,我注意到存在html内容(调用getPdf()函数的当前phtml文件的内容)和pdf自身的内容
$vr = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$vr->setNoRender(true);
$layout = Zend_Layout::getMvcInstance();
$layout->disableLayout();
但不幸的是,它在magento1.7中不起作用。然后,我尝试通过删除头函数的这两行来响应$pdfString的内容像 它只包含Pdf自己的内容。然后我意识到html内容的出现是由于这两行代码
header("Content-Disposition: attachment; filename=mydownloads.pdf");
header("Content-type: application/x-pdf");
有人能帮我解决这个问题吗。因此,PDF文档将在下载文件夹中生成。如何从pdf文档中删除html内容?创建pdf文件
要在您选择的服务器文件夹中创建PDF文档,可以使用以下方法:
$sImagePath = 'C:\Users\Hp\Desktop\landscape3.jpg';
$sPdfPath = 'C:\Users\Hp\Desktop\my.pdf';
$oPdf = new Zend_Pdf();
$oPage = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$oImg = Zend_Pdf_Image::imageWithPath($sImagePath);
$oPage->drawImage($oImg, 40, 764, 240, 820);
$oPdf->pages[] = $oPage;
$sContent = $oPdf->render();
file_put_contents($sPdfPath, $sContent);
提供下载链接
您似乎还希望为用户提供一个链接,以便他们可以在创建PDF后下载它。请参阅以了解更多关于此的信息
使用控制器创建和下载的示例
注意,这是一个快速而肮脏的黑客行为,滥用Mage\u Cms\u IndexController
只是为了快速证明和演示所使用的原理。无论如何,将其移植到自定义控制器应该是小菜一碟
在新的Magento 1.7.0.2实例上测试并运行良好,使用FF15和IE9:
class Mage_Cms_IndexController extends Mage_Core_Controller_Front_Action
{
const PDF_PATH = '/home/user/public_html/magento-1-7-0-2/';
public function indexAction($coreRoute = null)
{
// Create the PDF file
$sImagePath = '/home/user/public_html/magento-1-7-0-2/media/catalog/category/furniture.jpg';
$sPdfPath = self::PDF_PATH . 'myfurniture.pdf';
$oPdf = new Zend_Pdf();
$oPage = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$oImg = Zend_Pdf_Image::imageWithPath($sImagePath);
$oPage->drawImage($oImg, 40, 764, 240, 820);
$oPdf->pages[] = $oPage;
$sContent = $oPdf->render();
file_put_contents($sPdfPath, $sContent);
// Echo the download link
echo '<a href="cms/index/download/pdf/myfurniture">Download myfurniture.pdf</a>';
return;
/*
$pageId = Mage::getStoreConfig(Mage_Cms_Helper_Page::XML_PATH_HOME_PAGE);
if (!Mage::helper('cms/page')->renderPage($this, $pageId)) {
$this->_forward('defaultIndex');
}
*/
}
public function downloadAction()
{
// Cancel if the `pdf` param is missing
if (!$sPdfName = $this->getRequest()->getParam('pdf', false)) {
return $this->_forward('noRoute');
}
// Sanitize passed value and form path
$sPdfName = preg_replace('[a-z0-9]', '', $sPdfName);
$sPdfPath = self::PDF_PATH . $sPdfName . '.pdf';
// Cancel if file doesn't exist or is unreadable
if (!file_exists($sPdfPath) || !is_readable($sPdfPath)) {
return $this->_forward('noRoute');
}
// Set proper headers
header('Content-Type: application/pdf');
header("Content-Disposition: attachment; filename=" . urlencode($sPdfName . '.pdf'));
header('Content-Transfer-Encoding: binary');
header("Content-Length: " . filesize($sPdfPath));
// Send
readfile($sPdfPath);
}
// :
}
文件,调整路径,然后开始测试
加载Magento安装的主页时,应该会显示PDF的下载链接。我已经尝试了“如何使PDF文件在HTML链接中可下载”中指定的解决方案?它不工作也会抛出相同的错误。我希望PDF文档应该自动保存在浏览器的下载文件夹中。您是否知道如何从pdf文档中删除html内容?您是否已经证明在服务器上创建pdf文件的方法确实可以生成正确的pdf文件?您的服务器似乎在Windows下运行,因此双击您创建的PDF文件应该会告诉您该文件是否正常,否则您的Acrobat Reader将无法打开它。如果它是一个合适的PDF文件,那么您的问题是关于下载(而不是生成)PDF文件。确切地说,问题是关于下载PDF而不是生成PDF。感谢您所做的努力,上述代码在“app/code/core/Mage/Cms/controllers/IndexController.php”中运行良好。。。但我想从自定义控制器中的PDF文档中删除html内容,而不是“app/code/core/Mage/Cms/controllers/IndexController.php”文件中的内容。
class Mage_Cms_IndexController extends Mage_Core_Controller_Front_Action
{
const PDF_PATH = '/home/user/public_html/magento-1-7-0-2/';
public function indexAction($coreRoute = null)
{
// Create the PDF file
$sImagePath = '/home/user/public_html/magento-1-7-0-2/media/catalog/category/furniture.jpg';
$sPdfPath = self::PDF_PATH . 'myfurniture.pdf';
$oPdf = new Zend_Pdf();
$oPage = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$oImg = Zend_Pdf_Image::imageWithPath($sImagePath);
$oPage->drawImage($oImg, 40, 764, 240, 820);
$oPdf->pages[] = $oPage;
$sContent = $oPdf->render();
file_put_contents($sPdfPath, $sContent);
// Echo the download link
echo '<a href="cms/index/download/pdf/myfurniture">Download myfurniture.pdf</a>';
return;
/*
$pageId = Mage::getStoreConfig(Mage_Cms_Helper_Page::XML_PATH_HOME_PAGE);
if (!Mage::helper('cms/page')->renderPage($this, $pageId)) {
$this->_forward('defaultIndex');
}
*/
}
public function downloadAction()
{
// Cancel if the `pdf` param is missing
if (!$sPdfName = $this->getRequest()->getParam('pdf', false)) {
return $this->_forward('noRoute');
}
// Sanitize passed value and form path
$sPdfName = preg_replace('[a-z0-9]', '', $sPdfName);
$sPdfPath = self::PDF_PATH . $sPdfName . '.pdf';
// Cancel if file doesn't exist or is unreadable
if (!file_exists($sPdfPath) || !is_readable($sPdfPath)) {
return $this->_forward('noRoute');
}
// Set proper headers
header('Content-Type: application/pdf');
header("Content-Disposition: attachment; filename=" . urlencode($sPdfName . '.pdf'));
header('Content-Transfer-Encoding: binary');
header("Content-Length: " . filesize($sPdfPath));
// Send
readfile($sPdfPath);
}
// :
}
app/code/core/Mage/Cms/controllers/IndexController.php