开发小课堂 二, 生成pdf

qiqiminmin UID.638527
2015-06-03 发表

本帖最后由 qiqiminmin 于 2015-6-3 10:31 编辑

首先我要忍不住的吐槽一下微软,在windows runtime下,居然没有一个可以生成pdf的方案(免费的方法),其他方法基本上是商业的,大多是600美元到1000美元。

我在xamarin上一直用itextsharp, android和ios, wp7, wp8,都可以使用,即使不能使用,自己修改源代码也能很好一直,基本无缝链接,唯独wp8.1,win8.1没有一点办法(虽然可以使用,但是通过 app kit验证,太多api通不过),连pdfsharp都可以在wp7上跑。对于win8.1,wp8.1,win10,有种漠然的感动。

不过我教大家的,又是一种有趣的方法,o(^▽^)o。利用javascript实现 pdf的打印 (Xaml 写成 html 我想有点经验的难度应该不打)
主要流程如下

【1】在 Page上加一个 webView

【2】在工程下新建一个html文件夹,放入. 记得要设置 复制到输出文件夹, 直接往不能附件 js文件,但是 jspdf.js是很容易找到的。
jspdf.js, index.html


【3】在C#端,加上以下代买
[mw_shl_code=csharp,true]async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
//throw new NotImplementedException();
var path =Windows.ApplicationModel.Package.Current.InstalledLocation.Path;
Uri url = web.BuildLocalStreamUri("MyTag","HTML.html");
StreamUriWinRTResolver myResolver = new StreamUriWinRTResolver();
web.LoadCompleted += web_LoadCompleted;
web.NavigateToLocalStreamUri(url, myResolver);
}



void web_NavigationFailed(object sender, WebViewNavigationFailedEventArgs e)
{
var s = web;
//throw new NotImplementedException();
}

void web_LoadCompleted(object sender, NavigationEventArgs e)
{
var s = web;

var res = web.InvokeScript("getdata", new string[]{htmlstring, "200","200","{}"});
//throw new NotImplementedException();
}[/mw_shl_code]

[mw_shl_code=csharp,true]public sealed class StreamUriWinRTResolver : IUriToStreamResolver
{
public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
{
if (uri == null)
{
throw new Exception();
}
string path = uri.AbsolutePath;

// Because of the signature of the this method, it can't use await, so we
// call into a seperate helper method that can use the C# await pattern.
return GetContent(path).AsAsyncOperation();
}

private async Task<IInputStream> GetContent(string path)
{
// We use a package folder as the source, but the same principle should apply
// when supplying content from other locations
try
{
Uri localUri = new Uri("ms-appx:///html" + path);
StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri);
IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read);
return stream;
}
catch (Exception) { throw new Exception("Invalid path"); }
}
}[/mw_shl_code]

【4】在 index.html里可以随意加入函数。。。。
<script>
function getdata(a,d,e,h)
{
var doc = new jsPDF();

doc.fromHTML(a, d, e, h);
var res = doc.output('datauristring');
return res;
}
</script>

【5】在 C# 那边调用的时候就很简单了
var res = webView.InvokeScript("getdata",new string[]{htmltstring,"200","200","{}"});

返回的数据就是这个样子的
'data:file/pdf;base64,/9j/4AAQSkZJRgA‘
base64以后的就是文件的base64格式,转换成byte[],就可以直接使用了。。。。。。o(^▽^)o。。。。。
微软,你害的我们太苦了,为什么连个pdf都不支持,还要我求救于javascript.

标签: 微软 开发 课堂

敬告:
为防止不可控的内容风险,本站已关闭新用户注册,新贴的发表及评论;
你现在看到的内容只是互联网用户曾经发表的言论快照,仅用于老用户留存纪念,且仅与科技行业相关,全部内容不代表本站观点及立场;
本站重新开放前已针对包括用户隐私、版权保护、信息安全、国家政策在内的各种互联网法律法规要求,执行了隐患内容的自查、屏蔽和删除;
本站目前所属个人主体,未有任何盈利安排与计划,且与原WFUN.COM所属公司不存在任何关联关系;
如果本帖内容或者相关资源侵犯到您的合法权益,或者您认为存在问题,那么请您务必点此举报或投诉!
本站使用Golang构建,点击此处申请开源鄂ICP备18029942号-4联系站长投诉/举报