大家好,我有一个使用 Spring Boot 将代码转换为异步代码的用例。
@Service
public class TaggedPdfReaderToolone extends TaggedPdfReaderTool {
@Autowired
ViewTagUtils viewTagUtils;
protected int order;
/**
* Constructs a {@link TaggedPdfReaderTool} via a given {@link PdfDocument}.
*
* @param document the document to read tag structure from
*/
public TaggedPdfReaderToolone(PdfDocument document) {
super(document);
}
@Override
public TaggedPdfReaderToolone setRootTag(String rootTagName) {
this.rootTag = rootTagName;
order =0;
return this;
}
@Override
@Async
public void convertToXml(OutputStream os)
throws IOException {
convertToXml(os, "UTF-8");
}
@Override
@Async
public void convertToXml(OutputStream os, String charset)
throws IOException {
out = new OutputStreamWriter(os, Charset.forName(charset));
if (rootTag != null) {
out.write("<" + rootTag + ">" + System.lineSeparator());
}
// get the StructTreeRoot from the document
PdfStructTreeRoot structTreeRoot = document.getStructTreeRoot();
if (structTreeRoot == null)
throw new PdfException(KernelExceptionMessageConstant.DOCUMENT_DOES_NOT_CONTAIN_STRUCT_TREE_ROOT);
// Inspect the child or children of the StructTreeRoot
inspectKids(structTreeRoot.getKids());
out.write("</page> ");
if (rootTag != null) {
out.write("</" + rootTag + ">");
}
out.flush();
out.close();
}
}
我正在其他类中使用上述类中的方法
@Slf4j
@Service
public class Viewtag {
@Autowired
ViewTagUtils viewTagUtils;
public void gettagcontent(String taggedPdf, String xmlfilepath) throws IOException {
log.info("Viewing tagged page content started "+ taggedPdf);
PdfReader reader = new PdfReader(taggedPdf);
// log.info("Before file load.");
//filePathBean.setFilePath(taggedPdf);
PdfDocument doc = new PdfDocument(reader);
log.info("After file load.");
viewTagUtils.setPagNumber(0);
if (doc.isTagged()) {
log.info("view tagged content called");
log.info(xmlfilepath);
FileOutputStream xmlOut = new FileOutputStream(xmlfilepath);
// new TaggedPdfReaderTool(doc).setRootTag("root").convertToXml(xmlOut);
TaggedPdfReaderToolone taggedPdfReaderToolone = new
TaggedPdfReaderToolone(doc);
taggedPdfReaderToolone.setRootTag("root").convertToXml(xmlOut);
}else
log.info("PDF is not a tagged pdf.");
doc.close();
log.info("Viewing tagged page content completed "+ taggedPdf);
}
}
我在考虑创建 bean PdfDocument 时遇到错误
遇到此错误后我已完成配置 如下图,
@Configuration
public class BeanCreation {
@Bean
public PdfDocument createPdfDocument(String filePath) throws IOException {
return new PdfDocument(new PdfReader(filePath));
}
}
配置完成后 现在我收到错误了
考虑在BeanCreation中创建一个bean java.lang.String,
注意:- 我正在工作的项目是 gradel build 并且 没有 xml 文件 使用已完成。
解决问题的任何意见都将有助于充分 谢谢。
您正在注册 PdfDocument 类的单个实例。 String
filePath
是它的 Bean 依赖项,因此您也需要注册它,这很可能不是您想要的。我假设您需要为动态文件路径创建一个 pdf 文档。
不要注册 PdfDocument 类型的 Bean,而是注册中间服务层(例如 PdfGeneratorService)的 Bean,它将公开一个 API,该 API
takes
所需的路径并将生成委托给您的 PdfDocument。