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

这是我的 HTML 到 PDF 代码。我收到此错误

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)

请帮忙。

4

4 回答 4

1

从外观上看,您正在解析的 HTML 似乎不正确,因此错误 itextpdf 报告

在继续之前,您需要纠正此问题。

您可以做的一件事是使用 chrome 或 mozilla firefox 上的开发人员选项来查找 html 代码中的问题。这对你有很大帮助

于 2019-11-20T15:36:57.840 回答
0

对于这个问题:当文件关闭时,将相关页面写入文件。但是HTML可能有一些问题,例如你的html不正确,所以无法解析到正确的页面,itextpdf会报告这个错误,确保你有正确的html,应该没问题。

于 2013-09-16T12:17:28.350 回答
0

它看起来相当复杂。尝试使用另一个库。以下是代码示例:

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

这是HTML to PDF JAVA API

如果您需要使用元数据:

{
         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);
      }

对于加密:

 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);
      }
于 2018-02-06T18:17:32.027 回答
-1

在 Document.. document.newPage() 上调用 newPage 方法

于 2013-09-16T10:38:08.360 回答