package testpdf;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
public class HtmlToPdf {
public static void main(String[] args)
throws IOException {
try {
String content = readFile("c:/temp/html.html", StandardCharsets.UTF_8);
OutputStream file = new FileOutputStream(new File("C:/temp/Test.pdf"));
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();
InputStream is = new ByteArrayInputStream(content.getBytes());
XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
document.close();
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}
libraries
https://drive.google.com/file/d/0B6md5yaDmFoCMHlKX2lZbUhBdjQ/view?usp=sharing
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
public class HtmlToPdf {
public static void main(String[] args)
throws IOException {
try {
String content = readFile("c:/temp/html.html", StandardCharsets.UTF_8);
OutputStream file = new FileOutputStream(new File("C:/temp/Test.pdf"));
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();
InputStream is = new ByteArrayInputStream(content.getBytes());
XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
document.close();
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}
libraries
https://drive.google.com/file/d/0B6md5yaDmFoCMHlKX2lZbUhBdjQ/view?usp=sharing
Comments
Post a Comment