如何解决将html转换为pdf
| 我正在尝试将linux的html转换为pdf,也必须在Web APP中使用它,请让我知道有哪些可用的工具。 到目前为止,我已经尝试过 html2ps htmlfilename > a.ps
ps2pdf a.ps > a.pdf
但是上面没有转换图像并且忽略了CSS。我的开发环境是linux(RHEL5)
我也尝试过http://www.webupd8.org/2009/11/convert-html-to-pdf-linux.html我收到此错误
[root@localhost bin]# ./wkhtmltopdf www.example.com a.pdf
./wkhtmltopdf: error while loading shared libraries: libQtWebKit.so.4: cannot open shared object file: No such file or directory
解决方法
您所走的路是正确的:wkhtmltopdf是执行此操作的最简单方法。请注意,存储库中的代码可能已过时(不确定此软件包的最新信息);您可能需要从源代码进行编译,或者获取静态链接的版本(该版本很大,但是已经包含QT库和其他依赖项)。
另外,根据您的情况,您可能只是缺少一个库-在此处安装ѭ2do可能会有用。
,易于实现并且适合将HTML + CSS转换为pdf的两种方法是。
1)将\“ Jspdf javascript \”插件与\“ html2canvas plugin \”(Web应用程序)一起使用。
插入稳定版本的jspdf插件。
var script = document.createElement(\'script\');
script.type = \'text/javascript\';
script.src =\'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.0.272/jspdf.min.js\';
document.head.appendChild(script);
插入html2canvas插件
var script = document.createElement(\'script\');
script.type = \'text/javascript\';
script.src = \'https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js\';
document.head.appendChild(script);
插入以下脚本
var html2obj = html2canvas($(\'your div class here\'));
var queue = html2obj.parse();
var canvas = html2obj.render(queue);
var img = canvas.toDataURL(\"image/jpg\");
console.log(img);
var doc=new jsPDF(\"p\",\"mm\",\"a4\");
var width = doc.internal.pageSize.width;
var height = doc.internal.pageSize.height;
doc.addImage(canvas,\'JPEG\',15,35,180,240,\'SLOW\');
doc.save(\"save.pdf\");
IE 11的特殊情况
document.getElementById(\"your div here\").style.backgroundColor = \"#FFFFFF\";
2)使用wkhtmltopdf
从这里安装wkhtmltopdf
我们可以直接从terminal / commandLine使用wkhtmltopdf,但是在Java语言的情况下,我们可以使用包装器。
使用wkhtmltopdf包装器的代码示例
import com.github.jhonnymertz.wkhtmltopdf.wrapper.Pdf;
import com.github.jhonnymertz.wkhtmltopdf.wrapper.page.PageType;
import com.github.jhonnymertz.wkhtmltopdf.wrapper.params.Param;
public class PofPortlet extends MVCPortlet {
@Override
public void render(RenderRequest request,RenderResponse response) throws PortletException,IOException
{ super.render(request,response);
Pdf pdf = new Pdf();
pdf.addPage(\"http://www.google.com\",PageType.url);
// Add a Table of contents
pdf.addToc();
// The \"wkhtmltopdf\" shell command accepts different types of options such as global,page,headers and footers,and toc. Please see \"wkhtmltopdf -H\" for a full explanation.
// All options are passed as array,for example:
// Save the PDF
try {
pdf.saveAs(\"E:\\\\output.pdf\");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3)其他工具包括phantom.js,itextpdf,grabz.it
,可能最简单的方法是启动任何现代浏览器,转到站点,然后使用浏览器的“打印”功能打印到pdf(假设您的系统已设置pdf打印机)。不过,我不知道这是否是您的选择,并且这种事情在Web应用程序中无法正常运行。不过,您可能想尝试一下。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。