PaulBickford
How to access a Go library (Pdfcpu) in Elixir using Wasmex?
I have an Elixir module in which I am trying to call out to Pdfcpu to modify existing PDFs. Pdfcpu is written in Go and has a CLI and an API, and I am trying to use the API. I am using Wasmex to run the WASM code and interface with Elixir.
I have created a simple Go module:
package main
import (
"log"
"github.com/pdfcpu/pdfcpu/pkg/api"
)
func main() {
}
func RotateFile(inFile, outFile string, rotation int, selectedPages []string) {
if err := api.RotateFile(inFile, outFile, rotation, []string{"1"}, nil); err != nil {
log.Fatal(err)
}
}
then ran
go mod init proj/pdfcpu
go get github.com/pdfcpu/pdfcpu@latest
go mod tidy
GOOS=wasip1 GOARCH=wasm go build -o main.go
To test, I ran a simple Elixir script
api_binary = File.read!("main.wasm")
{:ok, api_pid} = Wasmex.start_link(%{bytes: api_binary, wasi: true})
Wasmex.call_function(api_pid, "RotateFile", ["input.pdf", "output.pdf", 180, [], nil])
And I receive {:error, "exported function `RotateFile` not found"}
RotateFile is in the api package, so I tried calling “api.RotateFile” instead of “RotateFile”, but it made no difference.
What am I doing wrong?
First Post!
Vidar
This might or might not be relevant, but anyway: I try to validate and challenge the Elixir skill packages with new projects, and this seemed like a new one with wasm and everything. I’m not a Go person, and Claude insisted using Go with WASM and Elixir had at least 2-3 hurdles to pass before getting there. Hence the switch to Rust and WASM and Elixir, the Extism library for the bridging, and the Rust lopdf library for the pdf.
The showcase is working with a Phoenix Liveview to upload pdf files, various operation per the library, and a preview pane showing the page for rotation and delete etc.
If you can use it for anything it is at GitHub - BadBeta/Experimental_Elixir_Rust_lopdf_wasm_showcase · GitHub .
The production Docker release did not go smooth, so I got my own purpose fulfilled - hole in Claude skills located. (Will update once it is fixed). Having a PDF library around is handy so that is nice too, although I think I prefer a Rustler NIF to the WASM setup.
Any particular reason for the WASM setup? Portability, crash safety?
Most Liked
Vidar
It was a new one to me, but Claude insisted this was the one to use for Elixir Rust WASM setup. So as I’m exploring anyway I figured lets give it a try.
Vidar
Yes, I figured PDF was common enough to just to give it a background run. Learning some about WASM and this Extism library/ framework was also interesting.
A Rustler NIF will likely be many times faster than going with WASM. On the other hand WASM seems like a good solution for portability and crash safety. In general I’ve found that Elixir and Rust work very well together.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #elixirconf-us
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









