Java iText cann';t使用google驱动器文件的链接转换HTML
我使用iText将HTML转换为PDF,并将PDF写入我的谷歌硬盘。我的代码运行良好。 但是,当我在谷歌硬盘中使用带有文件链接的HTML时,iText生成了一个没有文件链接的PDF文件!如图所示: 应该是这样的:(当我在chrome中打开html字符串时): 有人知道为什么iText不转换Google驱动器文件链接吗?或者问题到底出在哪里 我的代码:Java iText cann';t使用google驱动器文件的链接转换HTML,java,itext,Java,Itext,我使用iText将HTML转换为PDF,并将PDF写入我的谷歌硬盘。我的代码运行良好。 但是,当我在谷歌硬盘中使用带有文件链接的HTML时,iText生成了一个没有文件链接的PDF文件!如图所示: 应该是这样的:(当我在chrome中打开html字符串时): 有人知道为什么iText不转换Google驱动器文件链接吗?或者问题到底出在哪里 我的代码: // my html string String htmlBody = "<html> <head></he
// my html string
String htmlBody = "<html>
<head></head>
<body>
<meta charset="utf-8" />
<div dir="ltr">
hallo
<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" />
<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]
//我的html字符串
字符串htmlBody=”
你好
这是我的医生:
萨拉姆
";
//使用iText将html转换为pdf:
ByteArrayOutputStream pdfStream=新建ByteArrayOutputStream();
文档=新文档();
PdfWriter writer=PdfWriter.getInstance(文档,pdfStream);
document.open();
CSSResolver CSSResolver=XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
HtmlPipelineContext htmlContext=新MySpecialImageProviderWarehtmlPipelineContext();
setTagFactory(Tags.getHtmlTagProcessorFactory());
setImageProvider(新的MyImageProvider());
PdfWriterPipeline pdf=新的PdfWriterPipeline(文档、编写器);
HtmlPipeline html=新的HtmlPipeline(htmlContext,pdf);
CssResolverPipeline css=新的CssResolverPipeline(cssResolver,html);
XMLWorker=newxmlworker(css,true);
XMLParser p=新的XMLParser(worker);
p、 parse(新的ByteArrayInputStream(htmlBody.getBytes(“UTF-8”)),Charset.forName(“UTF-8”);
document.close();
[//使用iText输出在我的google drive中创建pdf:
File fileData=新文件();
setName(“我的pdf文件”);
setId(newFileId);
setMimeType(“application/pdf”);
AbstractInputStreamContent in=new ByteArrayContent(fileData.getMimeType(),pdfStream.toByteArray());
File File=googleDriveService.files().create(fileData,in.execute();][1]
从HTML字符串中删除所有CSS时会发生什么?实际上,HTML字符串来自电子邮件。我尝试将电子邮件转换为pdf格式,并将其保存在我的谷歌硬盘中。所以html字符串是自动生成的,或者取决于电子邮件消息。在我的问题中,我把它写成字符串,只是为了避免编写很长的代码。当我使用gmail中的(使用驱动器插入文件)按钮在我的电子邮件消息中添加驱动器文件时,在电子邮件(html电子邮件)中生成的带有css的链接。当我从html字符串中删除css时,代码运行良好!!!它可能是XMLWorker无法处理的一些“特殊”CSS。您应该尝试使用iText 7和pdfHtml附加组件,它有更好的CSS支持。