简体   繁体   English

使用DOMPDF创建PDF并重定向

[英]Create PDF with DOMPDF and Redirect

In a PHP project I need to Create a PDF file and redirect to another page when user clicks a Submit button. 在一个PHP项目中,我需要创建一个PDF文件并在用户单击“提交”按钮时重定向到另一个页面。 I have managed to create the pdf file using DOMPDF . 我设法使用DOMPDF创建了pdf文件。 PDF creation is done in a seperate file ('PDFRecipt.php') . PDF创建是在单独的文件('PDFRecipt.php') I have called that page when a user clicks a button on the main page. 当用户单击主页上的按钮时,我已调用该页面。 This is how call PDF page 这就是调用PDF页面的方式

header('location:PDFRecipt.php');

but the problem is when I try to redirect after calling PDF page by 但是问题是当我通过调用PDF页面后尝试重定向时

header('location:Other.php');

It does not create the PDF (only redirects). 它不会创建PDF(仅重定向)。 I tried changing header('location:PDFRecipt.php'); 我尝试更改header('location:PDFRecipt.php'); to include_once('PDFRecipt.php'); 包括include_once('PDFRecipt.php');

then it does not create the PDF correctly (Corrupted PDF File) 则无法正确创建PDF(PDF文件损坏)

How to create the PDF file & redirect to other page? 如何创建PDF文件并重定向到其他页面?

EDIT: 编辑:

Code in PDFRecipt.php PDFRecipt.php代码

$html='SOME HTML';
include("../../dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream('FileName.pdf');        
//header('location:Other.php);

Answer to question 回答问题

For this to work you would need to move the second header call into the PDFRecipt file. 为此,您需要将第二个header调用移到PDFRecipt文件中。 At the moment with both of them in the one file your second call to header is overriding the first. 目前,它们都在一个文件中,您对header的第二次调用将覆盖第一个。

Remember that headers are sent when the output is sent to the users browser, which is why you often see people calling exit() right after a header('Location: http://example.org'); 请记住,在将输出发送到用户浏览器时会发送标头,这就是为什么您经常看到人们在header('Location: http://example.org');之后直接调用exit() header('Location: http://example.org'); .

So any subsequent calls to set the same header, in this case Location , will override the first until the headers are sent. 因此,任何后续的设置相同标头的调用(在本例中为Location )将覆盖第一个,直到发送标头为止。

It is also worth pointing out that you should be using full web URLs in Location : 还值得指出的是,您应该在Location使用完整的Web URL:

HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. HTTP / 1.1需要绝对URI作为»位置的参数:包括方案,主机名和绝对路径,但是某些客户端接受相对URI。 You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself 通常,您可以使用$ _SERVER ['HTTP_HOST'],$ _ SERVER ['PHP_SELF']和dirname()从您自己的亲戚那里创建绝对URI

according to the header page in the manual: http://php.net/manual/en/function.header.php 根据手册的header页: http : //php.net/manual/en/function.header.php

Update from comments 来自评论的更新

So you are using the stream() method to send the client the PDF - you cannot combine this with a Location: header. 因此,您正在使用stream()方法向客户端发送PDF-您无法将其与Location:标头结合使用。 This is because DOMPDF has already flushed content to screen. 这是因为DOMPDF已经将内容刷新到屏幕了。 I had assumed that your PDFRecipt.php file was storing the PDF to disk somewhere. 我以为您的PDFRecipt.php文件将PDF存储到磁盘上的某个地方。

See DOMPDF source code for more details: http://code.google.com/p/dompdf/source/browse/trunk/dompdf/lib/class.pdf.php#3061 有关更多详细信息,请参见DOMPDF源代码: http : //code.google.com/p/dompdf/source/browse/trunk/dompdf/lib/class.pdf.php#3061

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

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