简体   繁体   English

我们可以在 php 的两个 html 页面之间显示 .pdf 文件吗?

[英]Can we display .pdf file in between two html pages in php?

I have two html pages which are converted to pdf.我有两个转换为 pdf 的 html 页面。 Now I have a pdf file and I want to add it between the two converted html pages .现在我有一个 pdf 文件,我想将它添加到两个转换后的 html 页面之间。

I have tried converting the pdf to html but the tcpdf doesn't support this conversion of css and js.我曾尝试将 pdf 转换为 html,但 tcpdf 不支持 css 和 js 的这种转换。

Please help me.请帮我。

use setasign\Fpdi;
require_once('tcpdf_include.php');
require_once('../FPDI/src/autoload.php');
// Original file with multiple pages 
$fullPathToFile = '../sample.pdf';
class Pdf extends Fpdi\TcpdfFpdi{
    var $_tplIdx;
}
$pdf = new pdf('P', 'px', 'A4', true, 'UTF-8', false);
$page1='<table cellpadding="5" cellspacing="2" style="font-size:11px;font-family:Arial;">
</table>';
$pdf->writeHTMLCell($colWidth,300,$margin,$pagetopmargin,$page1);
$page2='';
$pdf->AddPage();
//$pdf->writeHTMLCell(235,232,495,10,$manageLogo);
$pdf->writeHTMLCell($colWidth,300,$margin,$pagetopmargin,$page2);
// THIS IS WHERE YOU GET THE NUMBER OF PAGES
$pdf -> setSourceFile('../sample.pdf');
$temp =$pdf->importPage(1);
$pdf->AddPage();   
$pdf->useTemplate($temp);
$pdf->SetMargins(30, 60, 50, true);
$pdf -> setSourceFile('../sample.pdf');
$temp =$pdf->importPage(2);
$pdf->AddPage();
$pdf->useTemplate($temp ,-4, 30);
$page3=' <h4>Ongoing Rent Payments</h4>
<p>Our preferred payment method for rent is Rental Rewards. (Please note that there is a small convenience fee charged for the use of the system. These fees are charged by a third-party payment processor – Rental Rewards</p> <p>The fees for the convenience of the use of the services are: (must be able to set-up fee at agent level, as there is variation)</p> <ol type="a"  style="margin-left: 20px;">';
$pdf->AddPage();
$pdf->writeHTMLCell($colWidth,300,$margin,$pagetopmargin-20,$page3);
ob_end_clean();
$pdf->Output('tenancy_application.pdf');

PDF needs a pdf reader program installed, so embedding in html is limited by browser/OS support. PDF 需要安装 pdf 阅读器程序,因此嵌入 html 受浏览器/操作系统支持的限制。 Normally you can open a pdf file in a browser tab, so opening in an iframe might also work (not tested).通常,您可以在浏览器选项卡中打开 pdf 文件,因此在 iframe 中打开也可能有效(未经测试)。

<iframe src="link to pdf"></iframe>

To embed the PDF within the page you could use pdf.js from Mozilla.要在页面中嵌入 PDF,您可以使用 Mozilla 的pdf.js。 This would allow you to use a JavaScript snippet to pull in and render your PDF like so:这将允许您使用 JavaScript 片段来拉入并呈现您的 PDF,如下所示:

// If absolute URL from remote server is provided, configure CORS header on that server.
var url = '//cdn.mozilla.net/pdfjs/helloworld.pdf';

PDFJS.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js';

// Asynchronous download of PDF
var loadingTask = PDFJS.getDocument(url);
loadingTask.promise.then(function(pdf) {

  // Fetch the first page
  var pageNumber = 1;
  pdf.getPage(pageNumber).then(function(page) {
    // Set scale
    var scale = 1.5;
    var viewport = page.getViewport(scale);

    // Prepare canvas using PDF page dimensions
    var canvas = document.getElementById('the-canvas');
    var context = canvas.getContext('2d');
    canvas.height = viewport.height;
    canvas.width = viewport.width;

    // Render PDF page into canvas context
    var renderContext = {
      canvasContext: context,
      viewport: viewport
    };
    var renderTask = page.render(renderContext);
  });
}, function (reason) {
  console.error(reason);
});

If you wanted to process your PDF upfront, rather than waiting until runtime you could convert it into HTML first by using a file conversion API such as Zamzar .如果您想预先处理您的 PDF,而不是等到运行时,您可以先使用文件转换 API(例如Zamzar)将其转换为 HTML。 You could use the API to programatically convert from PDF to HMTL (from PHP) and then use that inline.您可以使用 API 以编程方式从 PDF 转换为 HMTL(从 PHP),然后使用该内联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM