2016-04-12 13 views
1

CSSスタイルの複雑なHTMLファイルをAndroidアプリケーションのPDFファイルに変換する必要があるという要件があります。 Android用にITextライブラリを使用しようとしましたが、CSSスタイルと一部のタグを解釈できません。私はたくさんの検索をしており、自分の要求を満たすライブラリを見つけることができませんでした。CSSスタイルを持つ複雑なHTMLファイルをAndroidのPDFファイルに変換する

AndroidでHTMLファイルをPDFに変換するライブラリはありますか?以下は

以下

<div id=divCont style="left:0px;top:0px;width:auto;height:569px;"> 
 
    ## 
 
    <div id="draggable_Name" class="ui-widget-content" style="border: 0px solid black;position:absolute;font-family:Arial;font-size:10pt;font-weight:normal;margin-left:210px;margin-top:45px;width:252px;height:27px;"> #Name-Data#</div> 
 
    ## 
 
    <div id="draggable_Age_Gender" class="ui-widget-content" style="border: 0px solid black;position:absolute;font-family:Arial;font-size:10pt;font-weight:normal;margin-left:210px;margin-top:82px;width:252px;height:27px;"> #AgeGender-Data#</div> 
 
    ## 
 
    <div id="draggable_Diagnosis" class="ui-widget-content" style="border: 0px solid black;position:absolute;font-family:Arial;font-size:10pt;font-weight:normal;margin-left:210px;margin-top:151px;width:252px;height:52px;line-height:0.5cm;"> #Diagnosis-Data#</div> 
 
    ## 
 
    <div id="draggable_Note" class="ui-widget-content" style="border: 0px solid black;position:absolute;font-family:Arial;font-size:10pt;font-weight:normal;margin-left:17px;margin-top:120px;width:102px;height:252px;line-height:0.7cm;">#Note-Data#</div> 
 
    ## 
 
    <div id="draggable_Rx_Text" class="ui-widget-content" style="border: 0px solid black;padding-bottom:15px;position:absolute;font-family:Arial;font-size:10pt;font-weight:normal;margin-left:245px;margin-top:249px;width:277px;height:252px;line-height:0.7cm;">#Prescription-Data#</div> 
 
    ## 
 
    <div id="draggable_Additional_Information" class="ui-widget-content" style="border: 0px solid black;position:absolute;font-family:Arial;font-size:10pt;font-weight:normal;margin-left:210px;margin-top:517px;width:277px;height:52px;line-height:0.7cm;">#AdditionalInformation-Data#</div> 
 
    ## 
 
    <div id="draggable_Date" class="ui-widget-content" style="border: 0px solid black;position:absolute;font-family:Arial;font-size:10pt;font-weight:normal;margin-left:1px;margin-top:5px;width:122px;height:27px;">#Date-Data#</div> 
 
    ## 
 
    <div id="draggable_Signature" class="ui-widget-content" style="border: 0px solid black;padding-bottom:5px;position:absolute;font-family:Arial;font-size:10pt;font-weight:normal;margin-left:10px;margin-top:574px;width:122px;height:27px;">#Signature-Data#</div> 
 
    ## 
 
    <div id="draggable_DrName" class="ui-widget-content" style="border: 0px solid black;padding-bottom:5px;position:absolute;font-family:Arial;font-size:10pt;font-weight:normal;margin-left:12px;margin-top:645px;width:150px;height:25px;">#DrName-Data#</div> 
 
    ## 
 
    <div id="draggable_MedDocket_Id" class="ui-widget-content" style="border: 0px solid black;position:absolute;font-family:Arial;font-size:9pt;font-weight:normal;margin-left:210px;margin-top:2px;width:252px;height:32px;">#MedDocketID-Data#</div> 
 
    ## 
 
    <div id="draggable_doctor_RegistrationNo" class="ui-widget-content" style="border: 0px solid black;position:absolute;font-family:Arial;font-size:9pt;font-weight:normal;margin-left:8px;margin-top:507px;width:172px;height:27px;">#doctor_RegistrationNo-Data#</div> 
 
    ## 
 
    <div id="draggable_Patient_Address" class="ui-widget-content" style="border: 0px solid black;position:absolute;font-family:Arial;font-size:9pt;font-weight:normal;margin-left:1px;margin-top:63px;width:202px;height:52px;">#Patient_Address-Data#</div> 
 
    ## 
 
    <div id="draggable_Weight" class="ui-widget-content" style="border: 0px solid black;position:absolute;font-family:Arial;font-size:9pt;font-weight:normal;margin-left:210px;margin-top:114px;width:72px;height:32px;">#Weight-Data#</div> 
 
    ## 
 
    <div id="draggable_Prescription_SerialNo" class="ui-widget-content" style="border: 0px solid black;position:absolute;font-family:Arial;font-size:9pt;font-weight:normal;margin-left:1px;margin-top:32px;width:122px;height:27px;">#SerialNo-Data#</div> 
 
    ## 
 
</div>

c5.html私のhtmlファイルは、私はPDFファイルに私のhtmlを変換するのに使用されるコードです。

private void createPDF(File pdfFile) { 
    // create a new document 
    Document document = new Document(PageSize.A4); 
    try { 
     FileOutputStream fos = new FileOutputStream(pdfFile); 
     PdfWriter pdfWriter = PdfWriter.getInstance(document, fos); 
     document.open(); 

     File htmlFile = new File(docDir, "c5.html"); 
     FileInputStream fis = new FileInputStream(htmlFile); 
     String html = convertStreamToString(fis); 

     InputStream stream = new ByteArrayInputStream(html.getBytes("UTF-8")); 

     // get the XMLWorkerHelper Instance 
     XMLWorkerHelper worker = XMLWorkerHelper.getInstance(); 
     // convert to Pdf 
     worker.parseXHtml(pdfWriter, document, stream);   
     // close the document 
     document.close(); 
     // close the writer 
     pdfWriter.close(); 

     Intent intent = new Intent(Intent.ACTION_VIEW); 
     Uri uri = Uri.fromFile(pdfFile); 
     intent.setDataAndType(uri, "application/pdf"); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
     startActivity(intent); 
    } catch (DocumentException e) { 
     e.printStackTrace(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

public String convertStreamToString(InputStream is) throws IOException { 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 
    String line; 
    while ((line = reader.readLine()) != null) { 
     sb.append(line).append("\n"); 
    } 
    reader.close(); 
    return sb.toString(); 
} 
+0

は私たちに、CSSとHTMLのあなたに問題を与え、あなたが使用しているコードを表示しています。 –

答えて

0

チェックアウトクラスjsPDF、最新バージョンはv1.81(2015年12月20日

http://www.fpdf.org/en/download.php

+0

返信mleggに感謝しますが、私はアンドロイドアプリのpdfファイルにhtmlファイルを変換する必要がありますので、phpライブラリ、つまりfpdfまたはjsPDFは役に立ちません。 –

+0

これを試してみましたか?[url to pdf] https:// play.google.com/store/apps/details?id=com.nop.urltopdf&hl=en – mlegg

+0

ここは、今年のトップ5、https://pdf.wondershare.com/のリンクです – mlegg

関連する問題