将dompdf与绝对链接一起使用 [英] Using dompdf with absolute links

查看:103
本文介绍了将dompdf与绝对链接一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用标准的DOMPDF代码来呈现现有网页(例如, 1 ):

I am using the standard DOMPDF code to render existing web pages (e.g.1):

$dompdf = new DOMPDF();
$dompdf->set_base_path($artpath);
$dompdf->load_html_file($artpath);
$dompdf->render();
$dompdf->stream($pdfpath);

其中 $ artpath '是通往HTML代码和 $ pdfpath 是PDF的名称。

where $artpath' is the path to the HTML code and $pdfpath is the name of the PDF.

但是,网页包含两个相对链接(正确遵循)和绝对链接(例如 /gifs/bullet.gif )。这可能是因为DOMPDF代码正在 http://www.epress中执行。 ac.uk/src/xtra/makeapdf.php www.epress.ac.uk 是我服务器上的虚拟域,该服务器还托管该虚拟域 jasss.soc.surrey.ac.uk (也就是说,两个域都在相同服务器上)。似乎DOMPDF应该使用 www.epress.ac.uk 的文档根,而应该使用 jasss.surrey的文档根.ac.uk

However, the web page contains both relative links (which are correctly followed) and absolute links (e.g. /gifs/bullet.gif) which are not found. This is probably because the DOMPDF code is being executed at http://www.epress.ac.uk/src/xtra/makeapdf.php, www.epress.ac.uk being a virtual domain on my server, which also hosts the virtual domain jasss.soc.surrey.ac.uk (that is, both domains are on the same server). It would seem that DOMPDF is using the document root of www.epress.ac.uk, when it should be using the document root of jasss.surrey.ac.uk.

有没有解决的办法?我尝试将 $ _ SERVER ['DOCUMENT_ROOT'] 重置为 jasss.soc.surrey.ac.uk ,然后调用 new DOMPDF(),但这似乎无法解决问题。我收到如下错误:

Is there some way around this? I have tried resetting $_SERVER['DOCUMENT_ROOT'] to the document root of jasss.soc.surrey.ac.uk before calling new DOMPDF(), but this doesn't seem to solve the problem. I get errors such as:

file_get_contents(/styles/jasssarticle.css) [function.file-get-contents]: failed to open stream: No such file or directory

Unable to load css file /styles/jasssarticle.css

根据wwwvalidator.w3.org,该网页是有效的HTML

The web page is valid HTML according to the www validator.w3.org

感谢您的建议!

推荐答案

您正在通过文件系统加载文件。这意味着所有对外部文件的引用(在路径中不包含域部分)都是相对于文件系统呈现的。您可以通过三种方式引用文件:

You're loading a file via the file system. That means all references to external files that don't include a domain part in the path are rendered relative to the file system. You can reference files in three ways:


  • 完整的URL(包括域),例如 http://example.com/image.png

  • 绝对路径,例如 /file/path/image.png 。相对于文件系统的根而不是网站的根或用户的主目录(在共享主机的情况下)读取。

  • 相对路径(无斜杠),例如 file / path / image.png 。这是相对于HTML文件读取的。因此,在您的情况下,将从 /Volumes/Documents/VirtualSites/jasss/16/2/file/path/image.png 中读取文件。

  • Full URL (including domain), e.g. http://example.com/image.png. These are always read from the URL specified.
  • Absolute path, e.g. /file/path/image.png. This is read relative to the root of the file system, not the root of the web site, or the user's home directory (in the case of shared hosting).
  • Relative path (no leading slash), e.g. file/path/image.png. This is read relative to the HTML file. So in your case the file would be read from /Volumes/Documents/VirtualSites/jasss/16/2/file/path/image.png.

调用 $ dompdf-> set_base_path()仅会影响相对路径。

Calling $dompdf->set_base_path() only affects the relative path.

您必须修改绝对文件引用以包含网站根目录的路径,例如 /Volumes/Documents/VirtualSites/jass/styles/jasssarticle.css ,或通过网站加载文件,例如 http://jasss.soc.surrey.ac.uk/16/2/1.html

You'll have to modify the absolute file references to include the path to the website root, e.g. /Volumes/Documents/VirtualSites/jasss/styles/jasssarticle.css, or load the file via the website, e.g. http://jasss.soc.surrey.ac.uk/16/2/1.html.

这篇关于将dompdf与绝对链接一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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