Linux.com

Home Learn Linux Linux Documentation Linux How Tos Converting HTML to PDF Using Java and Qt

Converting HTML to PDF Using Java and Qt

 

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();

}

}

 

Comments

Subscribe to Comments Feed

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board