嗨,我正在使用飞碟pdf创建 Spring 开机pdf文件。我想在一个新选项卡上打开生成的pdf文件,而不是在user/tmp上获取创建的文件(我使用的是ubuntu)。
组成部分:
@Component
public class PdfGenaratorUtil {
@Autowired
private TemplateEngine templateEngine;
public void createPdf(String templateName, Map map) throws Exception {
Assert.notNull(templateName, "The templateName can not be null");
Context ctx = new Context();
if (map != null) {
Iterator itMap = map.entrySet().iterator();
while (itMap.hasNext()) {
Map.Entry pair = (Map.Entry) itMap.next();
ctx.setVariable(pair.getKey().toString(), pair.getValue());
}
}
String processedHtml = templateEngine.process(templateName, ctx);
FileOutputStream os = null;
String fileName = UUID.randomUUID().toString();
try {
final File outputFile = File.createTempFile(fileName, ".pdf");
os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(processedHtml);
renderer.layout();
renderer.createPDF(os, false);
renderer.finishPDF();
System.out.println("PDF created successfully");
}
finally {
if (os != null) {
try {
os.close();
} catch (IOException e) { /*ignore*/ }
}
}
}
}
我的控制器:
@GetMapping("/export/{idficha}")
public void export(@PathVariable int idficha) throws Exception {
Map<String, Object> cab = atMedService.getDatosPacientePdf(idficha);
Map<String,String> data = new HashMap<String,String>();
data.put("nombre",cab.get("nombre").toString());
data.put("rut", cab.get("rut").toString());
data.put("edad", cab.get("edad").toString());
data.put("prevision", cab.get("prevision").toString());
pdfGenaratorUtil.createPdf("pdf/ate-medica",data);
}
暂无答案!
目前还没有任何答案,快来回答吧!