Android Webview到PDF忽略作业名称
我试图使用中提供的示例代码从KitKat(4.4.4)上的Web视图打印HTML页面。因此,我将打印作业名称设置为:Android Webview到PDF忽略作业名称,android,pdf,printing,webview,Android,Pdf,Printing,Webview,我试图使用中提供的示例代码从KitKat(4.4.4)上的Web视图打印HTML页面。因此,我将打印作业名称设置为: String jobName = getString(R.string.app_name) + " Document"; PrintJob printJob = printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build()); 我的
String jobName = getString(R.string.app_name) + " Document";
PrintJob printJob = printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
我的代码运行良好,页面按预期打印。即使用户在Android标准打印对话框中选择“另存为PDF”选项,代码也会呈现一个漂亮的PDF,用户可以选择文件名。此时,我希望Android使用存储在jobName
字段中的字符串作为文件名。相反,它总是使用webview
作为文件名,尽管我的代码不包含webview
作为字符串
有没有办法设置另一个默认名称来存储在我的应用程序中生成的PDF文件
谢谢你的提示
更新:
我花了一些额外的时间研究这个问题,发现WebView
使用AwPrintDocumentAdapter
作为打印适配器,如果适配器是通过调用API文档中建议的createPrintDocumentAdapter()
创建的。然后,该类调用
PrintDocumentInfo.Builder(“webview”)
这似乎就是PDF总是被命名为“webview”的原因。一些研究在AwPrintDocumentAdapter
中揭示了以下内容:
@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
CancellationSignal cancellationSignal, LayoutResultCallback callback,
Bundle metadata) {
mAttributes = newAttributes;
// TODO(sgurun) pass a meaningful string once b/10705082 is resolved
PrintDocumentInfo documentInfo = new PrintDocumentInfo
.Builder("webview")
.build();
// TODO(sgurun) once componentization is done, do layout changes and
// generate PDF here, set the page range information to documentinfo
// and call onLayoutFinished with true/false depending on whether
// layout actually changed.
callback.onLayoutFinished(documentInfo, true);
}
因此,这似乎是我的问题的根本原因-至少如果代码进入我的Nexus4测试设备。。。最后,处理此命名问题的最佳方法似乎是自定义打印适配器实现
是否有其他解决方案不需要自定义打印适配器(其中应包含用于计算页数等的代码)?+1第一个问题很好!欢迎来到StackOverflow。