1

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

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

其中$artpath' 是 HTML 代码的路径,$pdfpath是 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.

有没有办法解决这个问题?我曾尝试重置$_SERVER['DOCUMENT_ROOT']jasss.soc.surrey.ac.ukbefore call的文档根目录new DOMPDF(),但这似乎并不能解决问题。我收到以下错误:

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

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

谢谢你的建议!

4

1 回答 1

4

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:

  • 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.

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

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.

于 2013-03-31T01:39:44.543 回答