【问题标题】:Converting Html content to a pdf file将 Html 内容转换为 pdf 文件
【发布时间】:2018-03-28 23:48:12
【问题描述】:

我有需要由某人填写的带有表单和画布的 html 文件(包括画布上的绘图)。
如何将填写表单后内容的html转换为PDF文件并将PDF保存在设备上?
我用谷歌搜索,但没有找到任何好的解决方案。

更新:所有的html、css、js文件都在assest文件夹中。

【问题讨论】:

    标签: android html pdf


    【解决方案1】:

    您可以使用iText

    首先你需要添加依赖——:

    dependencies {
    compile 'com.itextpdf:itextg:5.5.11'
    }
    

    示例代码

    private void createPDF (String pdfFilename){
    
      //path for the PDF file to be generated
      String path = "docs/" + pdfname;
      PdfWriter pdfWriter = null;
    
      //create a new document
      Document document = new Document();
    
      try {
    
       //get Instance of the PDFWriter
       pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(path));
    
       //document header attributes
       document.addAuthor("betterThanZero");
       document.addCreationDate();
       document.addProducer();
       document.addCreator("MySampleCode.com");
       document.addTitle("Demo for iText XMLWorker");
       document.setPageSize(PageSize.LETTER);
    
       //open document
       document.open();
    
       //To convert a HTML file from the filesystem
       //String File_To_Convert = "docs/SamplePDF.html";
       //FileInputStream fis = new FileInputStream(File_To_Convert);
    
       //URL for HTML page
       URL myWebPage = new URL("http://demo.mysamplecode.com/");
       InputStreamReader fis = new InputStreamReader(myWebPage.openStream());
    
       //get the XMLWorkerHelper Instance
       XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
       //convert to PDF
       worker.parseXHtml(pdfWriter, document, fis);
    
       //close the document
       document.close();
       //close the writer
       pdfWriter.close();
    
      }   
    
      catch (FileNotFoundException e) {
       e.printStackTrace();
      } catch (IOException e) {
       e.printStackTrace();
      } catch (DocumentException e) {
       e.printStackTrace();
      }       
    
     }
    

    编辑

    下面的代码可以让我从 assests 文件夹中获取 html 文件 -:

    InputStream is = getAssets().open("test.html");
    InputStreamReader fis =  new InputStreamReader(is);
    

    你可以改变

    URL myWebPage = new URL("http://demo.mysamplecode.com/");
    InputStreamReader fis = new InputStreamReader(myWebPage.openStream());
    

    InputStream is = getAssets().open("test.html");
    InputStreamReader fis =  new InputStreamReader(is);
    

    愿,帮助你。快乐编码:)

    【讨论】:

    • 你的依赖有问题。 android studio 向我显示一条错误消息:“无法解决:com.itextpdf:itextg:5.5.11”我尝试搜索 jar 文件,但也出现了同样的问题。你有想法吗?
    • 尝试使用 compile 'com.itextpdf:itextg:5.5.10' 这个对我有用
    • 我已经使用过这个库,这样一个很酷的库可以处理 pdf
    • 如果您在 marshmallow @tal 之上工作,您需要在清单文件中读取和写入权限以及需要运行时权限的异常情况@tal
    • android 工作室向我抛出 FileNotFoundException。查看我的更新问题
    【解决方案2】:

    https://github.com/blink22/react-native-html-to-pdf/blob/master/android/src/main/java/android/print/PdfConverter.java

    PdfConverter converter = PdfConverter.getInstance();
    File file = new File(Environment.getExternalStorageDirectory().toString(), "file.pdf");
    String htmlString = "html code here";
    converter.convert(getContext(), htmlString, file);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 2013-07-10
      • 1970-01-01
      相关资源
      最近更新 更多