iText无法使用google驱动器文件的链接转换HTML

时间:2017-03-16 14:36:23

标签: java itext

我使用iText将HTML转换为PDF并将PDF写入我的Google云端硬盘。我的代码很好用。 但是当我在我的Google云端硬盘中使用HTML链接文件时,iText会生成一个没有文件链接的PDF文件!如图所示:

generated pdf

应该是这样的:(当我在chrome中打开html字符串时):

enter image description here

任何人都知道为什么iText无法转换Google云端硬盘文件链接?或问题到底在哪里。

我的代码:

// my html string 
String htmlBody = "<html>
 <head></head>
 <body>
  <meta charset="utf-8" />
  <div dir="ltr">
   hallo&nbsp;
   <div>
    <br />
   </div>
   <div>
    here is my doc :
   </div>
   <div>
    ​<br />
    <div class="gmail_chip gmail_drive_chip" style="width:396px;height:18px;max-height:18px;background-color:#f5f5f5;padding:5px;color:#222;font-family:arial;font-style:normal;font-weight:bold;font-size:13px;border:1px solid #ddd;line-height:1">
    <a  href="https://docs.google.com/a/sotec.eu/document/d/1OEuQP2VdfonswF_TgyfRjzg2gBtFeP2b4tGrlYSMMUA/edit?usp=drive_web" target="_blank" style="display:inline-block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;text-decoration:none;padding:1px 0px;border:none;width:100%">
    <img style="vertical-align: bottom; border: none;" src="https://ssl.gstatic.com/docs/doclist/images/icon_11_document_list.png" />&nbsp;
        <span dir="ltr" style="color:#15c;text-decoration:none;vertical-align:bottom">test doc</span>
     </a>
    </div>​
   </div>
   <div>
    salam
   </div>
  </div> 
  <br /> 
 </body>
</html>";

// convert html to pdf using iText:
ByteArrayOutputStream pdfStream = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, pdfStream);
document.open();

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

HtmlPipelineContext htmlContext = new MySpecialImageProviderAwareHtmlPipelineContext();
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
htmlContext.setImageProvider(new MyImageProvider());

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

XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);

p.parse(new ByteArrayInputStream(htmlBody.getBytes("UTF-8")), Charset.forName("UTF-8"));

document.close();

[// use the iText output to create pdf in my google drive:

File fileData = new File();
fileData.setName("my pdf file");
fileData.setId(newFileId);
fileData.setMimeType("application/pdf");

AbstractInputStreamContent in =  new ByteArrayContent(fileData.getMimeType(), pdfStream.toByteArray());

File file = googleDriveService.files().create(fileData, in).execute();][1]

0 个答案:

没有答案