使用硒铬驱动程序生成PDF

使用硒铬驱动程序生成PDF

本文介绍了使用硒铬驱动程序生成PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试过用命令行:
$
为了生成HTML文件中的PDF,我想使用selenium Chrome驱动程序。 b $ b

  chrome.exe --headless --disable-gpu --print-to-pdf file:/// C:invoiceTemplate2.html 

它的工作原理非常完美,所以我想用JAVA做到这一点,这里是我的代码:

  System.setProperty(webdriver.chrome.driver,C:/work/chromedriver.exe); 
ChromeOptions选项=新ChromeOptions();
options.addArguments( - headless,--disable-gpu,--print-to-pdf,
file:/// C:/invoiceTemplate2.html) ;
WebDriver驱动程序=新的C​​hromeDriver(选项);
driver.quit();

服务器启动时没有问题,但是使用多个选项卡打开chrome,选项。



对此有何解决方案?你必须做两件事 -



首先 - 使用硒制作屏幕截图



其次 - 使用任何其他pdf工具转换该图片。第一步:从并将jar文件添加到您的构建路径中。



第2步:下面是代码片段

  ChromeOptions选项=新的ChromeOptions(); 
options.addArguments(disable-infobars);
options.addArguments( - print-to-pdf);

WebDriver驱动程序=新的C​​hromeDriver(选项);
driver.get(file:/// C:/invoiceTemplate2.html);

尝试{
文件截图=((TakesScreenshot)驱动程序).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(截图,新文件(screenshot.png));
Document document = new Document(PageSize.A4,20,20,20,20);
PdfWriter.getInstance(document,new FileOutputStream(webaspdf.pdf));
document.open();
Image image = Image.getInstance(screenshot.png);
document.add(image);
document.close();

$ b catch(异常e2){
// TODO自动生成的catch块
e2.printStackTrace();
}

注意:添加所需的itext包。上面使用的itext包是

  import com.itextpdf.text.Document; 
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
导入org.openqa.selenium.TakesScreenshot;


To generate PDF from a HTML file, I want to use selenium Chrome driver.

I tried it with command line :

chrome.exe --headless --disable-gpu --print-to-pdf   file:///C:invoiceTemplate2.html

and it works perfectly, So I wanted to do that with JAVA and here's my code :

System.setProperty("webdriver.chrome.driver", "C:/work/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless", "--disable-gpu", "--print-to-pdf",
            "file:///C:/invoiceTemplate2.html");
WebDriver driver = new ChromeDriver(options);
driver.quit();

The server is started with no problem, but chrome is opened with multiple tabs with the arguments I specified in Options.

Any solution to this ? thx.

解决方案

You have to do two things --

First - Make a screenshot using selenium

Second- convert that image using any other pdf tool itext . Here i am showing complete exmple to do this.

Step 1: Download the jar of itext from here and add the jar file to your build path.

Step 2: Below is the code snippet

    ChromeOptions options = new ChromeOptions();
    options.addArguments("disable-infobars");
    options.addArguments("--print-to-pdf");

    WebDriver driver = new ChromeDriver(options);
    driver.get("file:///C:/invoiceTemplate2.html");

    try{
        File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshot, new File("screenshot.png"));
        Document document = new Document(PageSize.A4, 20, 20, 20, 20);
        PdfWriter.getInstance(document, new FileOutputStream("webaspdf.pdf"));
        document.open();
        Image image = Image.getInstance("screenshot.png");
        document.add(image);
        document.close();

    }
    catch (Exception e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }

Note: Add the required package of itext. The packages of itext used above is

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;

这篇关于使用硒铬驱动程序生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 13:39