我想允许用户下载在iText中创建的pdf文件。
预期结果是用户可以将 pdf 下载到他们选择的目录。实际结果是文件无法下载。
我正在使用iText生成一个pdf,并将结果传递回我的ajax调用以允许用户下载文件。我有base64。我使用 https://base64.guru/converter/decode/pdf 检查它是否有效 这正确地显示了 pdf。但是,我无法在ajax".done"中下载结果。
我试过使用:
byte[] decoder = Base64.getDecoder().decode(b64);
在传回它之前;但是,我在".getDecoder(("上收到一条错误消息,"方法getDecoder((未定义类型为Base64
"。java代码是:
resourceImage = MySQLConnection.recipePDF(accountID, crID, servings, servingSize);
String imageDataString = Base64Encode2.encode(resourceImage);
System.out.println("imageDataString: " + imageDataString);
if (resourceImage == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No recipe pdf.");
} else {
String json = new Gson().toJson(imageDataString);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
}
上面的javascript/ajax代码是:
.done(function(responseJson1a){
dataType: "json";
alert(JSON.stringify(responseJson1a));
download(responseJson1a, "resourcefile.pdf", "application/pdf");
});
一个非常简单的答案。我需要添加"数据:应用程序/pdf;base64",以字符串开头。
download("data:application/pdf;base64,"+responseJson1a, "resourcefile.pdf", "application/pdf");