在我的应用程序中,仅在打印预览中打开pdf报告,该报告允许用户直接打印pdf文档。现在,我想使它自动化以验证pdf内容。
我已经通过base64中的api获得了pdf内容[已拆分以仅获取数据],我尝试在解码后转换为字节数组,但它仅打印垃圾字符。[字节数组为字符串] 现在,我已将这些数据转换为ByteBuffer并希望以pdf格式编写。
public static string GetConnectionString(string name)
{
string conStr = System.Environment.GetEnvironmentVariable($"ConnectionStrings:{name}",
EnvironmentVariableTarget.Process);
if (string.IsNullOrEmpty(conStr)) // Azure Functions App Service naming convention
conStr = System.Environment.GetEnvironmentVariable($"SQLAZURECONNSTR_{name}",
EnvironmentVariableTarget.Process);
return conStr;
}
有人可以告诉我如何将ByteBuffer的解码字节转换为pdf。
谢谢
答案 0 :(得分:1)
byte[] decodedBytes = new BASE64Decoder().decodeBuffer(str);
InputStream targetStream = new ByteArrayInputStream(decodedBytes);
PDDocument document = PDDocument.load(targetStream);
document.save("C:/test.pdf");
FileUtils.writeByteArrayToFile(new File("C:/test.pdf"), decodedBytes);
使用上述代码转换为pdf。
答案 1 :(得分:1)
从 API 获取 pdf 的 blob 数据:
Future<dynamic> getBlobdata(int pdfId) async {
try {
var response = await Dio().get(
"https://www.google.com/pdf/$pdfId",
options: Options(
responseType: ResponseType.bytes,
contentType: 'application/octet-stream',
),
);
var data = {"data": response.data.buffer};
return data;
} on DioError catch (error) {
var data = error.response.data;
return data;
}
}
定义文件名和保存文件的目录:
String fileName = "pdf$pdfId";
final dir = await getExternalStorageDirectory();
var pdfBlob = await getBlobdata(1); // have to be in a asyn func to use await
保存PDF:
final file = File("${dir.path}/$fileName.pdf");
await file.writeAsBytes(pdfBlob.asUint8List());
在应用中查看 Pdf :
await OpenFile.open("${dir.path}/$fileName.pdf");