使用DOMPDF创建PDF并重定向 [英] Create PDF with DOMPDF and Redirect

查看:111
本文介绍了使用DOMPDF创建PDF并重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个PHP项目中,我需要创建一个PDF文件,并在用户单击提交按钮时重定向到另一个页面。
我已经设法使用 DOMPDF 创建了pdf文件。 PDF创建是在单独的文件('PDFRecipt.php')中完成的。
当用户单击主页上的按钮时,我已经调用了该页面。这就是调用PDF页面的方式

In a PHP project I need to Create a PDF file and redirect to another page when user clicks a Submit button. I have managed to create the pdf file using DOMPDF. PDF creation is done in a seperate file ('PDFRecipt.php'). I have called that page when a user clicks a button on the main page. This is how call PDF page

header('location:PDFRecipt.php');

但是问题是,当我尝试通过调用PDF页面后尝试重定向时,

but the problem is when I try to redirect after calling PDF page by

header('location:Other.php');

它不会创建PDF(仅重定向)。我尝试将
header('location:PDFRecipt.php');
更改为
include_once('PDFRecipt .php');

It does not create the PDF (only redirects). I tried changing header('location:PDFRecipt.php'); to include_once('PDFRecipt.php');

然后它无法正确创建PDF(损坏的PDF文件)

then it does not create the PDF correctly (Corrupted PDF File)

如何创建PDF文件&重定向到其他页面?

How to create the PDF file & redirect to other page?

编辑:

<$ c $中的代码c> 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);


推荐答案

问题解答



要执行此操作,您需要将第二个标头调用移到 PDFRecipt 文件中。目前,他们两个都在一个文件中,您对 header 的第二次调用将覆盖第一个。

Answer to question

For this to work you would need to move the second header call into the PDFRecipt file. At the moment with both of them in the one file your second call to header is overriding the first.

记住标题是在输出发送到用户浏览器时发送的,这就是为什么您经常看到人们在 header('之后紧跟着呼叫 exit()的原因位置:http://example.org');

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');.

因此,任何随后的调用都将设置相同的标头,在这种情况下为 Location ,将覆盖第一个,直到发送标题为止。

So any subsequent calls to set the same header, in this case Location, will override the first until the headers are sent.

同样值得指出的是,您应该使用完整的位置中的Web URL:

It is also worth pointing out that you should be using full web URLs in Location:


HTTP / 1.1需要使用绝对URI作为自变量»位置:包括
的方案,主机名和绝对路径,但是某些客户端接受
相对URI。通常,您可以使用$ _SERVER ['HTTP_HOST'],
$ _SERVER ['PHP_SELF']和dirname()从自己的
亲戚中获取绝对URI

HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself

根据手册中的标头页: http://php.net/manual/en/function.header.php

因此,您正在使用 stream()方法向客户端发送PDF-您无法将其与 Location:标头结合使用。这是因为DOMPDF已经将内容刷新到屏幕了。我假设您的 PDFRecipt.php 文件将PDF存储到磁盘上的某个地方。

So you are using the stream() method to send the client the PDF - you cannot combine this with a Location: header. This is because DOMPDF has already flushed content to screen. I had assumed that your PDFRecipt.php file was storing the PDF to disk somewhere.

请参阅DOMPDF源代码更多详细信息: http:// code.google.com/p/dompdf/source/browse/trunk/dompdf/lib/class.pdf.php#3061

See DOMPDF source code for more details: http://code.google.com/p/dompdf/source/browse/trunk/dompdf/lib/class.pdf.php#3061

这篇关于使用DOMPDF创建PDF并重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆