Converting HTML to PDF Using Java and Qt

250

 

Here is the code:

import com.trolltech.qt.core.*;

import com.trolltech.qt.gui.*;

import com.trolltech.qt.webkit.*;

 

class html2pdf extends QWebView

{

private QPrinter printer = new QPrinter();

 

public html2pdf()

{

loadFinished.connect(this, “loadDone()”);

setHtml(“This is <b>HTML</b>”);

               // Or use load() to convert html page from url to pdf

}

 

public void loadDone()

{

printer.setPageSize(QPrinter.PageSize.A4);

printer.setOutputFormat(QPrinter.OutputFormat.PdfFormat);

printer.setOutputFileName(“test.pdf”);

print(printer);

System.out.println(“Done”);

QApplication.exit();

}

 

public static void main(String args[])

{

QApplication.initialize(args);

html2pdf h2p = new html2pdf();

h2p.show();

QApplication.exec();

}

}