You can use itext.jar which is freely available in net .by using it we can generate pdf from html or jsp or servlet.I have tried the similar think using servlet.
I have a problem while using itext for creating a pdf from database using servlet.While iam trying to open it 2nd time is is giving me error that the document has been closed you canot add any elements. This is because i have closed the 1st window.Please can any one provide me the solution for it.
package pdfConverter; /** *@author Syed Muhammad Aizaz Haider */
import java.io.*; import com.lowagie.text.*; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfWriter; public class Converter { public static void main (String pdf[]){ System.out.println("This is some text to be converted"); Document doc = new Document(); try{
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("RESULT.pdf")); doc.open(); BaseFont embeded = BaseFont.createFont("C:\\Windows\\Fonts\\comic.ttf",BaseFont.CP1250,BaseFont.EMBEDDED); Font font = new Font(embeded,35); font.setColor(25, 254, 75); doc.add(new Paragraph("Hello World",font)); doc.add(new Paragraph("Syed Muhammad Aizaz Haider Zaidee the Prince !!!",font)); doc.add(new Paragraph("abc.jpg")); doc.newPage(); Image img = Image.getInstance("abc.jpg"); System.out.println(img.getClass().getName()); doc.add(img);
package pdfConverter; /** *@author Syed Muhammad Aizaz Haider */ import com.lowagie.text.*; import com.lowagie.text.pdf.*; import java.io.*; public class FilesizeCompression {
public static void main(String[] args) { System.out.println("Chapter 8: example FileSizeComparison"); System.out.println("-> Creates a PDF file with a font that is not embedded"); System.out.println(" and compared it with files that have the font embedded."); System.out.println("-> jars needed: iText.jar"); System.out.println("-> resources needed: c:/windows/fonts/comic.ttf"); System.out.println("-> file generated: font__not_embedded.pdf"); System.out.println(" file generated: font_embedded1.pdf"); System.out.println(" file generated: font_embedded2.pdf");
// step 1 Document document1 = new Document(); Document document2 = new Document(); Document document3 = new Document(); try { // step 2 PdfWriter.getInstance(document1, new FileOutputStream( "font_not_embedded.pdf")); PdfWriter.getInstance(document2, new FileOutputStream( "font_embedded1.pdf")); PdfWriter.getInstance(document3, new FileOutputStream( "font_embedded2.pdf"));
// step 3: we open the document document1.open(); document2.open(); document3.open(); // step 4: BaseFont bf_not_embedded = BaseFont.createFont( "c:/windows/fonts/comic.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED); BaseFont bf_embedded = BaseFont.createFont( "c:/windows/fonts/comic.ttf", BaseFont.CP1252, BaseFont.EMBEDDED); Font font_not_embedded = new Font(bf_not_embedded, 12); Font font_embedded = new Font(bf_embedded, 12);
document1.add(new Paragraph( "This is document1 with ", font_not_embedded)); document2.add(new Paragraph( "quick brown fox jumps over the lazy dog", font_embedded)); document3.add(new Paragraph( "ooooo ooooo ooo ooooo oooo ooo oooo ooo", font_embedded)); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); }
// step 5: we close the document document1.close(); document2.close(); document3.close(); RandomAccessFileOrArray file; try { file = new RandomAccessFileOrArray("font_not_embedded.pdf"); System.out.println("Size font_not_embedded.pdf: " + file.length()); file = new RandomAccessFileOrArray("font_embedded1.pdf"); System.out.println("Size font_embedded1.pdf: " + file.length()); file = new RandomAccessFileOrArray("font_embedded2.pdf"); System.out.println("Size font_embedded2.pdf: " + file.length()); } catch (IOException e) { e.printStackTrace(); } }
If you would like to report an abuse of our service, such as a spam message, please . Если Вы хотите пожаловаться на содержимое этой страницы, пожалуйста .