根据HTML内容创建PDF文件-Android

时间:2019-07-15 23:29:03

标签: java android html pdf-generation html2pdf

我想将HTML字符串或HTML文件转换为PDF文件并将其保存在内存中。但是不知道怎么办?

我尝试了很多事情,包括以下内容,但我真的很困惑。

  • itext
  • html2pdf
  • xmlworker
  • pdfa

可以创建PDF文件,但是没有波斯字符和表格标签。

下面的代码:

public void create() {
    String FILE = Environment.getExternalStorageDirectory().toString() + "/AAA.pdf";

    try {
        Document document = new Document(PageSize.LETTER);
        PdfWriter.getInstance(document, new FileOutputStream(FILE));
        document.open();
        document.addAuthor("Real Gagnon");
        document.addCreator("Real's HowTo");
        document.addSubject("Thanks for your support");
        document.addCreationDate();
        document.addTitle("Please read this");

        HTMLWorker htmlWorker = new HTMLWorker(document);

        String str = "\n"
                + "<!DOCTYPE html>\n"
                + "<html dir=\"rtl\" lang=\"fa-IR\" prefix=\"og: http://ogp.me/ns#\"><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"
                + "<style type=\"text/css\">\n"
                + "<!--\n"
                + "@font-face { font-family: 'Persian1'; src: url('file:///android_asset/fonts/zarb.ttf'); }\n"
                + "@font-face { font-family: 'Persian2'; src: url('file:///android_asset/fonts/zarb.ttf'); }\n"
                + "        h1 {font-family: 'Persian2';}\n"
                + "        body {font-family: 'Persian1';}\n"
                + "        p {font-family: 'Persian1';}\n"
                + ".auto-style2 {\n"
                + "\tbackground-color: #DFDFDF;\n"
                + "}\n"
                + "*{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}*,*:before,*:after{background:transparent!important;color:#000 !important;-webkit-box-shadow:none!important;box-shadow:none !important;text-shadow:none !important}\n"
                + ".auto-style3 {\n"
                + "\ttext-align: justify;\n"
                + "}\n"
                + "-->\n"
                + "</style>\n"
                + "<head>\n"
                + "<title>آموزش نصب و کرک فتوشاپ Photoshop CC 2017 v18 - وب سایت آموزشی پی سی اید</title>\n"
                + "</head>\n"
                + "<body  dir=\"rtl\" class=\"rtl post-template-default single single-post postid-1460 single-format-standard wpb-js-composer js-comp-ver-4.12.1 vc_responsive js\" itemscope=\"itemscope\"itemtype=\"http://schema.org/WebPage\">\n"
                + "<div id=\"sb-site\" class=\"body_wraper_full\">\n"
                + "  <!-- begin content -->            \n"
                + "  <section id=\"content_main\" class=\"clearfix\">\n"
                + "<h1 >آشنایی با محیط فتوشاپ</h1>\n"
                + "</div>\n"
                + "</body></html>";

        htmlWorker.parse(new StringReader(str));
        document.close();
        System.out.println("Done");
        Toast.makeText(MainActivity.this, "done", Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

结果: enter image description here

0 个答案:

没有答案