2017-01-12 1 views
1

HTML을 PDF로 변환하려고합니다. 첫째, 다음 링크에서 HTML 코드를 XHTML로 변환했습니다. http://www.cruto.com/resources/code-generators/code-converters/html-to-xhtml.aspiText : html과 CSS를 Java에서 PDF로 변환

그런 다음이를 테스트하기 위해 생성 된 XHTML 코드가있는 HTML 파일을 만들어 브라우저에 성공적으로 표시했습니다. 그 후, 다음 Java 코드로 HTML 파일을 PDF로 변환하려고 시도했습니다.

public static final String DEST = "C:/Users/Turgut/Desktop/test12.pdf"; 
public static final String[] HTML = { "C:/Users/Turgut/Desktop/New Text Document (5).html", "C:/Users/Turgut/Desktop/New Text Document (5).html" }; 

public static void main(String[] args) { 

    File file = new File(DEST); 
    file.getParentFile().mkdirs(); 
    new TestHtmlToPdf().createPdf(DEST); 
} 

public void createPdf(String file) { 
    Document document = new Document(); 
    try { 
     //String HTML = "C:/Users/Turgut/Desktop/test12.html"; 
     PdfWriter.getInstance(document, new FileOutputStream(file)); 
     document.open(); 
     String css = readCSS(); 
     for (String htmlfile : HTML) { 
      String html = Utilities.readFileToString(htmlfile); 
      ElementList list = XMLWorkerHelper.parseToElementList(html, css); 
      for (Element e : list) { 
       document.add(e); 
      } 
      document.newPage(); 
     } 
     document.close(); 
    } 
    catch(IOException e) { 
     e.printStackTrace(); 
    } 
    catch(DocumentException ex) { 
     ex.printStackTrace(); 
    } 
} 

private String readCSS() throws IOException { 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    byte[] buffer = new byte[1024]; 
    int length; 
    InputStream is = XMLWorkerHelper.class.getResourceAsStream("/default.css"); 
    while ((length = is.read(buffer)) != -1) { 
     baos.write(buffer, 0, length); 
    } 
    return new String(baos.toByteArray()); 
} 

다음과 같이 head 태그에 스타일 태그가 있습니다.

<style type="text/css"> 
       body { 
       background-color: #FFFFFF; 
       font-family: 'Tahoma', "Times New Roman", Times, serif; 
       font-size: 11px; 
       color: #666666; 
       } 
       h1, h2 { 
       padding-bottom: 3px; 
       padding-top: 3px; 
       margin-bottom: 5px; 
       color : #000000; 
       text-transform: uppercase; 
       font-family: Arial, Helvetica, sans-serif; 
       } 
       h5 { 
       padding-bottom: 0px; 
       padding-top: 0px; 
       margin-top: 0px; 
       margin-bottom: 0px; 
       color : #000000; 
       font-style: normal; 
       font-family: Arial, Helvetica, sans-serif; 
       font-size: 1em; 
       text-transform:none; 
       } 
       h5x { 
       padding-bottom: 0px; 
       padding-top: 0px; 
       margin-top: 0px; 
       margin-bottom: 0px; 
       color : #000000; 
       font-style: bold; 
       font-family: Arial, Helvetica, sans-serif; 
       font-size: 1em; 
       text-transform:none; 
       }     
       h6 { 
       padding-bottom: 0px; 
       padding-top: 0px; 
       margin-top: 0px; 
       margin-bottom: 0px; 
       color : #666666; 
       font-style: normal; 
       font-family: Arial, Helvetica, sans-serif; 
       font-size: 1em; 
       text-transform:none; 
       } 

       h1 { 
       font-size: 1.4em; 
       text-transform:none; 
       } 
       h2 { 
       font-size: 1em; 
       color: brown; 
       } 
       h3 { 
       font-size: 1em; 
       color: #333333; 
       text-align: justify; 
       margin: 0; 
       padding: 0; 
       } 
       h4 { 
       font-size: 1.4em; 
       font-style: bold; 
       font-family: Arial, Helvetica, sans-serif; 
       color: #000000; 
       margin: 0; 
       padding: 0; 
       } 
       h4x { 
       font-size: 1.4em; 
       font-style: bold; 
       font-family: Arial, Helvetica, sans-serif; 
       color: #666666; 
       margin: 0; 
       padding: 0; 
       } 
       hr { 
       height:2px; 
       color: #000000; 
       background-color: #000000; 
       border-bottom: 1px solid #000000; 
       } 
       p, ul, ol { 
       margin-top: 1.5em; 
       } 
       ul, ol { 
       margin-left: 3em; 
       } 
       blockquote { 
       margin-left: 3em; 
       margin-right: 3em; 
       font-style: italic; 
       } 
       a { 
       text-decoration: none; 
       color: #70A300; 
       } 
       h7 { 
       font-size: 1.1em; 
       font-style: bold; 
       font-family: Arial, Helvetica, sans-serif; 
       color: #000000; 
       margin: 0; 
       padding: 0; 
       } 
       a:hover { 
       border: none; 
       color: #70A300; 
       } 
       #customerPartyTable { 
       border-width: 1px; 
       border-spacing: 0px; 
       border-style: solid; 
       border-color: #FFFFFF; 
       border-collapse: collapse; 
       background-color: #FFFFFF 
       } 
       #lineTable { 
       border-width:2px; 
       border-spacing:; 
       border-style: solid; 
       border-color: #000000; 
       border-collapse: collapse; 
       background-color:; 
       } 
       #lineTableTd { 
       border-width: 1px; 
       padding: 1px; 
       border-style: none solid none none; 
       border-color: black; 
       background-color: white; 
       } 
       #lineTableTh { 
       border-width: 1px; 
       padding: 1px; 
       border-style: none solid none none; 
       background-color: white; 
       } 
       #lineTableTrx { 
       border-width: 0px; 
       padding: 0px; 
       border-style: solid; 
       border-color: #000000; 
       background-color: white; 
       -moz-border-radius:; 
       } 
       #lineTableThx { 
       border-width: 1px; 
       padding: 1px; 
       border-style: none solid solid none; 
       background-color: white; 
       } 
       #lineTableTr { 
       border-width: 0px; 
       padding: 0px; 
       border-style: solid; 
       border-color: #000000; 
       background-color: white; 
       -moz-border-radius:; 
       } 
       #lineTableBudgetTd { 
       border-width: 0px; 
       border-spacing:0px; 
       padding: 1px; 
       border-style: solid; 
       border-color: #000000; 
       background-color: white; 
       -moz-border-radius:; 
       }      
       table { 
       border-spacing:0px; 
       } 
       td { 
       border-color:#000000; 
       }</style> 

HTML 파일에서 PDF 파일을 생성해도 문제가 없지만 CSS 블록을 읽을 수는 없습니다. 왜냐하면, PDF 파일과 HTML 파일은 같지 않기 때문입니다. PDF 파일에서 일부 텍스트 색상은 HTML 파일과 다릅니다.

어떻게 CSS로 PDF 파일을 생성 할 수 있습니까? 제안 해 주셔서 감사합니다.

답변

0
ByteArrayInputStream html = new ByteArrayInputStream(FileUtils.readFileToByteArray(new File(htmlSource))); 
    ByteArrayInputStream css = new ByteArrayInputStream(FileUtils.readFileToByteArray(new File(cssSource))); 
    html = getHtmlByteArrayStream(); //this is only for my picture not neccessary 
    // step 1 
    Document document = new Document(); 
    // step 2 
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(destPdf)); 
    writer.setInitialLeading(12); 
    // step 3 
    document.open(); 

    // step 4 
    XMLWorkerHelper.getInstance().parseXHtml(writer, document, html, css); 

    // step 5 
    document.close(); 

이것은 내가하는 방법입니다.

+0

@ Tksknl 해결책이 있습니까? – EthicsDev