用Go和WebAssembly进行浏览器端PDF处理
TLDR: This page demonstrates how to use WebAssembly (compiled from a tool called pdfcpu with Go v1.12) to extract the first page of a PDF file, done completely in client-side. Main idea is adding filesystem emulation support in Browser environment. To skip the prose below and go straight to the code, see the Github repo.
TLDR: 本页演示了如何使用WebAssembly(由Go v1.12的pdfcpu工具编译而成)来提取PDF文件的第一页,完全在客户端完成。主要的想法是在浏览器环境中添加文件系统模拟支持。要跳过下面的散文,直接看代码,请看Github repo。
WebAssembly (wasm) is gaining a lot of buzz these days. Many languages are starting to experiment with/adopt this new technology and add wasm compilation support. Rust (wasm-pack, wasm-bindgen), Go, C/C++ (emscripten), Java (TeaVM), .NET (blazor) are a few examples.
最近,WebAssembly(wasm)得到了很多人的关注。许多语言都开始尝试/采用这种新技术,并增加wasm编译支持。Rust(wasm-pack,wasm-bindgen)、Go、C/C++(emscripten)、Java(TeaVM)、.NET(blazor)就是几个例子。
In this article I am trying to document my attempt to port a command line application written in Go to wasm and using it in browser context. I've played with wasm using Emscripten for C/C++ codes before, but not with Go.
在这篇文章中,我试图记录我将一个用Go编写的命令行程序移植到wasm并在浏览器环境中使用的尝试。我曾经用Emscripten来处理C/C++代码的wasm,但没有用Go。
The wasm support for Go has landed in v1.11 and at the time of this writing the latest release version of Go is v1.12. It is said that the support is still maturing, with some limitations such as only the main
function being exported right now (see this issue), or not working on Android due to Out of Memory problem (issue). Things may improve and change in future release, so please keep that in mind when reading the steps below.
对围棋的wasm支持已经在v1.11版本中登陆,在写这篇文章的时候,围棋的最新发布版本是v1.12。据悉,该支持仍在不断成熟,有一些局限性,比如现在只有main
功能可以导出(见本期),或者由于内存不足的问题,在Android上无法使用(问题)。在未来的版本中,事情可能会得到改善和改变,所以在阅读下面的步骤时请记住这一点。
Also, I am not a Go programmer (I've written <100 LoC). In fact we are not even going to change a single line of Go code in this article (but there will be lots of JavaScript/Node.js involved, be warned!). If you are looking for how to call to JavaScript from Go in wasm, there are many o...