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

Print PDF

 

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 (0)Add Comment

Write comment
You must be logged in to post a comment. Please register if you do not have an account yet.

busy
Become an Individual Member

Who we are ?

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

More About the foundation...

Frequent Questions

Join / Members / Staff / Board