0
package example;

import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.html.simpleparser.StyleSheet;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

/*import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPTable;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.html.simpleparser.StyleSheet;
import com.lowagie.text.pdf.PdfWriter;*/

public class clas3 {
  public static void main(String[] args) throws Exception, IOException {
    Document document = new Document();
    StyleSheet st = new StyleSheet();
    st.loadTagStyle("body", "encoding", "30,10");
    PdfWriter.getInstance(document, new FileOutputStream("C:/Users/Jason/Desktop/html4.pdf"));
    document.open();
    try{HTMLWorker htmlWorker = new HTMLWorker(document); 
    FileReader xy=new FileReader("C:/Users/Jason/Desktop/SELECT Operation.htm");
    //String zy=String.format("<table><tr>"+xy+"</tr></table>"); 
    //StringReader hy= new StringReader(zy);



    PdfPCell cell;
    List p = new ArrayList();
    p = htmlWorker.parseToList(xy, st);
    Phrase p1 = new Phrase(); 

    for (int k = 0; k < p.size(); ++k){
         PdfPTable tb = (PdfPTable)((Element)p.get(k));
         tb.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
         System.out.println("1");
         document.add(tb);
         System.out.println(((Element)p.get(k)).toString());
         document.addTitle("jack");
         //p1.add((com.itextpdf.text.Element)p.get(k)); 
    }
    //cell = new PdfPCell(p1); 
    //document.add(cell);
    }catch(IOException e){}catch(Exception e){}
      //document.add((Element) p.get(k));}
    document.close();
  }
}

This is my HTML to PDF code.I am getting this error

Exception in thread "main" ExceptionConverter: java.io.IOException: The document has no pages.at com.itextpdf.text.pdf.PdfPages.writePageTree(PdfPages.java:113)
    at com.itextpdf.text.pdf.PdfWriter.close(PdfWriter.java:1217)
    at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:787)
    at com.itextpdf.text.Document.close(Document.java:420)
    at example.clas3.main(clas3.java:57)

Please help.

newuser
  • 8,338
  • 2
  • 25
  • 33
user1932600
  • 57
  • 3
  • 13
  • possible duplicate of [convert html to pdf using iText](http://stackoverflow.com/questions/20236257/convert-html-to-pdf-using-itext) – Autar Sep 17 '15 at 14:05
  • If you can do it at client end you can do it very easy. works perfect http://stackoverflow.com/questions/2255291/print-the-contents-of-a-div#answer-2255438 – Jinna Balu Mar 13 '17 at 19:06

4 Answers4

1

By the looks of it, it seems like the HTML you are parsing is incorrect and hence the error itextpdf reports

You would need to rectify this before moving forward.

One thing you could do is use the developer options on chrome or mozilla firefox to find issues in the html code. This would help you greatly

Standin.Wolf
  • 1,224
  • 1
  • 10
  • 32
0

For this issue: When document close, will write related page to file. but something may have some issue with HTML, e.g. you html is incorrect, so cannot parse to correct page then, itextpdf will report this error, make sure you have correct html, should be ok.

Bensson
  • 4,509
  • 4
  • 19
  • 24
0

It looks quite complicated. Try to use another library. Here is the example of the code:

import com.bcl.easypdf.printer.*;
import java.io.File;

public class TestPrinter
{
   public static void main(String[] args) throws Exception
   {
      if(args.length != 2)
         return;

      File inputFile = new File(args[0]);
      String inputFileName = inputFile.getCanonicalPath();

      File outputFile = new File(args[1]);
      String outputFileName = outputFile.getCanonicalPath();

      Printer printer = new Printer();
      printer.useLoader = false;
      try
      {
         IEPrintJob printjob = printer.getIEPrintJob();
         printjob.PrintOut(inputFileName, outputFileName);
      }
      catch(PrinterException ex)
      {
         System.out.println(ex.getMessage());
      }
      finally
      {
         printer.dispose();
      }
   }
}

And here is the HTML to PDF JAVA API

If you need to use metadata:

{
         IEPrintJob printjob = printer.getIEPrintJob();
         PDFSetting pdfSettings = printjob.getPDFSetting();
         pdfSettings.setMetaData(true);
         pdfSettings.setMetaDataTitle("title");
         pdfSettings.setMetaDataAuthor("authior");
         pdfSettings.setMetaDataSubject("subject");
         pdfSettings.setMetaDataKeywords("kw");
         pdfSettings.setMetaDataCreator("creator");
         printjob.PrintOut(inputFileName, outputFileName);
      }

For encryption:

 try
      {
         IEPrintJob printjob = printer.getIEPrintJob();
         PDFSetting pdfSettings = printjob.getPDFSetting();
         pdfSettings.setSecurity(true);
         pdfSettings.setSecurityUserPassword("123");
         pdfSettings.setSecurityOwnerPassword("456");
         pdfSettings.setSecurityEncryption(prnSecEncryption.PRN_SEC_ENCRYPT_128BITS);
         pdfSettings.setSecurityAnnotation(prnSecAnnotationPerm.PRN_SEC_ANNOT_PERM_FULL);
         pdfSettings.setSecurityExtraction(prnSecExtractionPerm.PRN_SEC_EXTR_PERM_FULL);
         pdfSettings.setSecurityModification(prnSecModificationPerm.PRN_SEC_MODIFY_PERM_FULL);
         pdfSettings.setSecurityPrinting(prnSecPrintingPerm.PRN_SEC_PRINT_PERM_FULL);
         printjob.PrintOut(inputFileName, outputFileName);
      }
-1

call the newPage method on Document.. document.newPage()

Balaji Krishnan
  • 1,007
  • 3
  • 12
  • 29