PaulBickford

PaulBickford

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?

Showing Posts 1 to 8

Vidar

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?

dimitarvp

dimitarvp

…extism library? :thinking:

Vidar

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.

dimitarvp

dimitarvp

Ah, this one: Extism · GitHub

antoine-duchenet

antoine-duchenet

Don’t you need the go:wasmexport directive here ?

Vidar

Vidar

Since I got some go feedback earlier anyways, the short of it was:

  • ‘go:wasmexport RotateFile’ needs to before the function as antoine says.
  • go:wasmexport has type restrictions so int must be int32, and string isn’t supported.
  • wasi: true alone does not give filesystem access.
  • log.Fatal calls os.Exit which kills the Wasmex instead of returning an error

I’ve tried checking and it all seems to be correct as of Go 1.26.

PaulBickford

PaulBickford OP

I will look your wasm_showcase, as well as just using a Rustler NIF. It looks like it could do the trick.

I have absolutely no reason for using WASM, Go or Pdfcpu, it just looked like an interesting solution (I have never worked with any of them). This is a just a small side project, but PDF manipulation comes up often enough that I would like to figure it out.

I found an article Running Go code from Elixir Using Web Assembly - Yasoob Khalid which suggests using pipes in Wasmex to get data in and out of WASM, which should allow strings. I will give it a go as well.

Thanks.

Vidar

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews