简体   繁体   English

如何在Java中使用wkhtmltopdf或wkhtmltoImage将HTML字符串转换为PDF?

[英]How to convert an HTML String to PDF using wkhtmltopdf or wkhtmltoImage in Java?

I am using wkhtmltoPDF to generate pdf from html string and It works fine for me as expected, but now I have to generate output as Image and I'm unable to find sample code or any library. 我正在使用wkhtmltoPDF从html字符串生成pdf,并且按预期对我来说效果很好,但是现在我必须将输出生成为Image,并且无法找到示例代码或任何库。

I have referred https://github.com/jhonnymertz/java-wkhtmltopdf-wrapper to generate pdf. 我已经推荐https://github.com/jhonnymertz/java-wkhtmltopdf-wrapper生成pdf。

import com.github.jhonnymertz.wkhtmltopdf.wrapper.Pdf;

public class WKhtmlToPdf
 { 
    Pdf pdf = new Pdf();
    pdf.addPageFromString("html code");
    pdf.addToc();

 try{
     pdf.saveAs("Output.pdf");
   }
    catch (IOException e)
     {
     }
    catch (InterruptedException e)
     {     
     }
 }

I wanted an output an Image. 我想要输出图像。 Can any one tell me and I don't want to use Runtime.getRuntime().exe() method to generate output. 谁能告诉我,并且我不想使用Runtime.getRuntime()。exe()方法生成输出。

Thanks in advance. 提前致谢。

wkhtmltopdf is a library which is part of the wkhtmltox project, which includes wkhtmltoimage too. wkhtmltopdfwkhtmltox项目的一部分,该库也包括wkhtmltoimage You can inspire yourself from the wrapper you linked above to develop a wkhtmltoimage wrapper. 您可以从上面链接的包装器中激发自己的灵感,以开发wkhtmltoimage包装器。

Also, I generally use ImageMagick to generate images programmatically. 另外,我通常使用ImageMagick以编程方式生成图像。 IM4Java is an example wrapper to it for Java. IM4Java是Java的示例包装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM