2016-04-12 4 views
1

Ich habe eine Anforderung, wo ich eine komplexe HTML-Datei mit CSS Stile in eine PDF-Datei in einer Android-App konvertieren muss. Ich habe versucht, IText Bibliothek für Android zu verwenden, aber es ist nicht in der Lage, die CSS Stile und einige der Tags zu interpretieren. Ich habe viel gesucht und konnte keine Bibliothek finden, die meine Anforderungen erfüllt.Konvertieren komplexer HTML-Datei, die CSS-Stile in eine PDF-Datei in Android

Gibt es eine andere Bibliothek zum Konvertieren von HTML-Datei in PDF in Android?

Unten ist mein html-Datei c5.html

<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>

Unten ist der Code, den ich verwendet, um meine html pdf-Datei zu konvertieren.

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

Zeigen Sie uns die CSS und HTML, die Sie Probleme gibt, und den Code, den Sie verwenden. –

Antwort

0

Check-out-Klasse jsPDF, ist die neueste Version v1.81 (2015-12-20

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

+0

Danke für die Antwort mlegg, aber ich muss HTML-Datei in PDF-Datei in meiner Android-App konvertieren. So Php libs dh fpdf oder jsPDF ist nicht nützlich für mich. –

+0

hast du es versucht? [Url zu pdf] https: // play.google.com/store/apps/details?id=com.nop.urltopdf&hl=de – mlegg

+0

hier ist ein Link zu den Top 5, dieses Jahr, https://pdf.wondershare.com/ pdf-creating-tips/android-web-zu-pdf.html – mlegg

Verwandte Themen