2017-10-17 9 views
2

J'ai cherché les questions et n'ai pas été capable de trouver une solution à mon problème spécifique. Ce que je dois faire est de convertir des fichiers HTML contenant des images et des styles CSS en PDF. J'utilise iText 5 et j'ai pu inclure le style dans le PDF généré. Cependant, je me bats encore, y compris les images. J'ai inclus mon code ci-dessous. L'image avec le chemin absolu est incluse dans le PDF généré, l'image avec le chemin relatif ne l'est pas. Je sais que je dois mettre en œuvre AbstractImageProvider, mais je ne sais pas comment le faire. Toute aide est grandement appréciée.Convertir du HTML avec des images en PDF en utilisant iText

Java Fichier:

public class Converter { 

    static String in = "C:/Users/APPS/Desktop/Test_Html/index.htm"; 
    static String out = "C:/Users/APPS/Desktop/index.pdf"; 
    static String css = "C:/Users/APPS/Desktop/Test_Html/style.css"; 

    public static void main(String[] args) { 
     try { 
      convertHtmlToPdf(); 
     } catch (DocumentException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    private static void convertHtmlToPdf() throws DocumentException, IOException { 
     Document document = new Document(); 
     PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(out)); 
     document.open(); 
     XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document, new FileInputStream(in), new FileInputStream(css)); 
     document.close(); 
     System.out.println("PDF Created!"); 
    } 

    /** 
    * Not sure how to implement this 
    * @author APPS 
    * 
    */ 
    public class myImageProvider extends AbstractImageProvider { 

     @Override 
     public String getImageRootPath() { 
      // TODO Auto-generated method stub 
      return null; 
     } 

    } 

} 

Html fichier:

<!DOCTYPE html> 
<html lang="en"> 

<head> 
    <title>HTML to PDF</title> 
    <link href="style.css" rel="stylesheet" type="text/css" /> 
</head> 

<body> 
    <h1>HTML to PDF</h1> 
    <p> 
     <span class="itext">itext</span> 5.4.2 
     <span class="description"> converting HTML to PDF</span> 
    </p> 
    <table> 
     <tr> 
      <th class="label">Title</th> 
      <td>iText - Java HTML to PDF</td> 
     </tr> 
     <tr> 
      <th>URL</th> 
      <td>http://wwww.someurl.com</td> 
     </tr> 
    </table> 
    <div class="center"> 
     <h2>Here is an image</h2> 
     <div> 
      <img src="images/Vader_TFU.jpg" /> 
     </div> 
     <div> 
      <img src="https://www.w3schools.com/images/picture.jpg" alt="Mountain" /> 
     </div> 
    </div> 
</body> 
</html> 

Css fichier:

h1 { 
    color: #ccc; 
} 

table tr td { 
    text-align: center; 
    border: 1px solid gray; 
    padding: 4px; 
} 

table tr th { 
    background-color: #84C7FD; 
    color: #fff; 
    width: 100px; 
} 

.itext { 
    color: #84C7FD; 
    font-weight: bold; 
} 

.description { 
    color: gray; 
} 

.center { 
    text-align: center; 
} 

Répondre

3

Ce qui suit est basé sur la version 5.5.12 iText5

Supposons que vous avoir ce directeur Structure y:

enter image description here

Avec ce code et en utilisant les dernières iText5:

package converthtmltopdf; 

import com.itextpdf.text.Document; 
import com.itextpdf.text.DocumentException; 
import com.itextpdf.text.pdf.PdfWriter; 
import com.itextpdf.tool.xml.XMLWorker; 
import com.itextpdf.tool.xml.XMLWorkerHelper; 
import com.itextpdf.tool.xml.html.Tags; 
import com.itextpdf.tool.xml.net.FileRetrieve; 
import com.itextpdf.tool.xml.net.FileRetrieveImpl; 
import com.itextpdf.tool.xml.parser.XMLParser; 
import com.itextpdf.tool.xml.pipeline.css.CSSResolver; 
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline; 
import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline; 
import com.itextpdf.tool.xml.pipeline.html.AbstractImageProvider; 
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline; 
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext; 
import com.itextpdf.tool.xml.pipeline.html.LinkProvider; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 

/** 
* 
* @author george.mavrommatis 
*/ 
public class ConvertHtmlToPdf { 
    public static final String HTML = "C:\\Users\\zzz\\Desktop\\itext\\index.html"; 
    public static final String DEST = "C:\\Users\\zzz\\Desktop\\itext\\index.pdf"; 
    public static final String IMG_PATH = "C:\\Users\\zzz\\Desktop\\itext\\"; 
    public static final String RELATIVE_PATH = "C:\\Users\\zzz\\Desktop\\itext\\"; 
    public static final String CSS_DIR = "C:\\Users\\zzz\\Desktop\\itext\\"; 

    /** 
    * Creates a PDF with the words "Hello World" 
    * @param file 
    * @throws IOException 
    * @throws DocumentException 
    */ 
    public void createPdf(String file) throws IOException, DocumentException { 
     // step 1 
     Document document = new Document(); 
     // step 2 
     PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); 
     // step 3 
     document.open(); 
     // step 4 

     // CSS 
     CSSResolver cssResolver = 
       XMLWorkerHelper.getInstance().getDefaultCssResolver(false); 
     FileRetrieve retrieve = new FileRetrieveImpl(CSS_DIR); 
     cssResolver.setFileRetrieve(retrieve); 

     // HTML 
     HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); 
     htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); 
     htmlContext.setImageProvider(new AbstractImageProvider() { 
      public String getImageRootPath() { 
       return IMG_PATH; 
      } 
     }); 
     htmlContext.setLinkProvider(new LinkProvider() { 
      public String getLinkRoot() { 
       return RELATIVE_PATH; 
      } 
     }); 

     // Pipelines 
     PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer); 
     HtmlPipeline html = new HtmlPipeline(htmlContext, pdf); 
     CssResolverPipeline css = new CssResolverPipeline(cssResolver, html); 

     // XML Worker 
     XMLWorker worker = new XMLWorker(css, true); 
     XMLParser p = new XMLParser(worker); 
     p.parse(new FileInputStream(HTML)); 

     // step 5 
     document.close(); 
    } 
    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) throws IOException, DocumentException { 
     // TODO code application logic here 
     new ConvertHtmlToPdf().createPdf(DEST); 
    } 

} 

Et voici le résultat:

enter image description here

Cet exemple utilise le code de: https://developers.itextpdf.com/examples/xml-worker-itext5/xml-worker-examples

Espérons que cela aide

+0

Cela a fonctionné parfaitement !! Merci beaucoup. – jdubicki

+0

@jdubicki content d'avoir pu aider. Ne pas oublier si une réponse vous aide, vous pouvez l'augmenter et ensuite l'accepter. Merci – MaVRoSCy

+0

J'ai un autre problème que j'ai besoin d'aide. J'ai dû faire quelques modifications afin de pouvoir lire et rendre les listes non ordonnées imbriquées. J'ai remarqué que mes balises d'en-tête ne sont pas analysées dans le fichier PDF. Quelqu'un peut-il m'aider à corriger cela? Voici les chages que j'ai besoin de faire. – jdubicki