Convert HTML to PDF using Qtwebkit and PHP

不言
Release: 2023-04-05 17:36:01
Original
3166 people have browsed it

Wkhtmltopdf是一个非常有用的应用程序,用于从html(网页)创建pdf。本篇文章将介绍关于如何使用php脚本和Linux命令行工具创建网页的pdf。

Convert HTML to PDF using Qtwebkit and PHP

步骤1:在Linux中安装wkhtmltopdf

从google code下载wkhtmltopdf并安装到Linux系统。

# cd /opt
# wget https://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-i386.tar.bz2
# tar xjf wkhtmltopdf-0.9.9-static-i386.tar.bz2
# mv wkhtmltopdf-i386 /usr/bin/wkhtmltopdf
# chown apache:apache /usr/bin/wkhtmltopdf
# chmod +x /usr/bin/wkhtmltopdf
Copy after login

步骤2:使用命令行创建PDF

首先检查wkhtmltopdf脚本,它在命令行中正常工作。下面的命令将创建http://google.com网页的pdf。

# /usr/bin/wkhtmltopdf http://google.com google.pdf
Copy after login

步骤3:使用wkhtmltopdf创建pdf的php代码

使用下面的PHP代码块从HTML(网页)生成PDF。此脚本需要为Apache启用shell_exec函数。大多数共享主机不允许此函数。

使用下面的代码创建一个文件名getpdf.php,并将其放到网站文档根目录中。

<?php
 
$url = $_GET['url'];    // Website URL to Create pdf
$name = $_GET['pdf'];   // Output pdf name
 
$command = "/usr/bin/wkhtmltopdf ";
$pdf_dir = "/var/www/html/pdfs/";     // Pdf files will be saved here
$ex_cmd = "$command $url " . $pdf_dir . $name;
$output = shell_exec($ex_cmd);
 
?>
Copy after login

打开以下网址生成网站的pdf(html)。

语法:http:
//youdomain.com/getPdf.php?url = &pdf =

示例:
https : //tecadmin.net/getPdf.php?url = http : //google.com&pdf=google.pdf

本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注PHP中文网的Linux视频教程栏目!

The above is the detailed content of Convert HTML to PDF using Qtwebkit and PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!