2017-12-20 5 views
4

나는 안드로이드 애플 리케이션을위한 HTML 문서를 HTML로 변환 iTextPdf (안드로이드를위한 iTextG) 라이브러리와 함께 일하고 있습니다. 영수증의 로고를 제외하고 모든 것이 잘 작동합니다. 내 HTML은안드로이드 ItextG 이미지 태그가 렌더링되지 않습니다

<img src="http...."></img> 

PDF 파일로 이미지를 가지고 있지 않습니다 생성 된 이미지의 소스 HTTP URL을 <img> 태그가 포함되어 있습니다. 자바 애플리케이션에서 실행되는 동일한 코드와 html이 작성된 PDF와 로고를 표시하고 있습니다 (이미지 액세스에 문제가 없음을 보여줍니다). 이 기능이 Java와 만 호환되지만 Android와 호환되지 않는지 궁금합니다. 나는 다음과 같은 종속성을 사용하여 사용하고 있습니다 :

compile 'com.itextpdf:itextg:5.5.10' 
compile 'com.itextpdf.tool:xmlworker:5.5.10' 

html로 코드 : 주요 활동에

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="English"> 
<head> 
    <title>Title</title> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
</head> 

<body> 
<img src="https://image.flaticon.com/teams/slug/google.jpg"></img> 
<h1>Fischerstube</h1> 
</body> 
</html> 

기능 :

private void htmlToPdf(String html) throws DocumentException, IOException { 

    try { 

     File file = new File(Environment.getExternalStorageDirectory() + File.separator + "logo.pdf"); 
     OutputStream fileOutputStream = new FileOutputStream(file); 
     Document document = new Document(); 
     document.setPageSize(new Rectangle(201,720)); 
     PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream); 
     document.open(); 
     InputStream is = new ByteArrayInputStream(html.getBytes()); 
     XMLWorkerHelper.getInstance().parseXHtml(writer, document, is); 
     document.close(); 
     fileOutputStream.close(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

그것은 <h1> 태그를 렌더링 Fischerstube을 표시하지만 ANDROIRD에 아무런 이미지를 장치. 아무쪼록이 일에 도움이 될 수 있습니까, 감사하게 생각합니다.

+1

iTextG와 XMLWorker의 버전이 동일해야합니다. –

+0

@AmedeeVanGasse : xmlworker와 itextg 모두 5.5.10으로 확인했는데 동일한 동작 (이미지 없음)이 있습니다. 이 조합은 Java에서는 잘 작동하지만 Android에서는 작동합니다. –

+0

@AmedeeVanGasse Android에서 사용중인 정보를 방금 업데이트했는지 확인하십시오. –

답변

0

제공되는 설명서를보고 here이 해결되었습니다.

매니페스트에 인터넷 권한이 있는지 확인하십시오.

class Base64ImageProvider extends AbstractImageProvider { 

    @Override 
    public Image retrieve(String src) { 
     int pos = src.indexOf("base64,"); 
     try { 
      if (src.startsWith("data") && pos > 0) { 
       byte[] img = Base64.decode(src.substring(pos + 7)); 
       return Image.getInstance(img); 
      } 
      else { 
       return Image.getInstance(src); 
      } 
     } catch (BadElementException ex) { 
      return null; 
     } catch (IOException ex) { 
      return null; 
     } 
    } 

    @Override 
    public String getImageRootPath() { 
     return null; 
    } 
} 

그런

public void createPdf() throws IOException, DocumentException { 
    String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
      "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" + 
      "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"English\">\n" + 
      "<head>\n" + 
      " <title>Title</title>\n" + 
      " <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/>\n" + 
      "</head>\n" + 
      "\n" + 
      "<body>\n" + 
      "<img src=\"https://image.flaticon.com/teams/slug/google.jpg\"></img>\n" + 
      "<h1>Fischerstube</h1>\n" + 
      "</body>\n" + 
      "</html>"; 


    // step 1 
    File file = new File(Environment.getExternalStorageDirectory() + File.separator + "logo.pdf"); 
    OutputStream fileOutputStream = new FileOutputStream(file); 
    Document document = new Document(); 
    // step 2 
    PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream); 
    // step 3 
    document.open(); 
    // step 4 

    // CSS 
    CSSResolver cssResolver = 
      XMLWorkerHelper.getInstance().getDefaultCssResolver(true); 

    // HTML 
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); 
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); 
    htmlContext.setImageProvider(new Base64ImageProvider()); 

    // Pipelines 
    PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer); 
    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf); 
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html); 

    // XML Worker 
    XMLWorker worker = new XMLWorker(css, true); 
    XMLParser p = new XMLParser(worker); 
    p.parse(new ByteArrayInputStream(str.getBytes())); 

    // step 5 
    document.close(); 
} 

당신이 배경 스레드에서 createPdf 방법을 실행해야합니다 PDF

에 행해져 Yout HTML로 변환하는 PDF 방법을 만들 호출 Base64ImageProvider 클래스를 만들 수 있습니다. 네트워크 작업을 수행 할 것이기 때문입니다.

관련 문제