Sample code for 30+ languages & platforms
Zig

Duplicate openssl pkcs12 –export –in certfile.cer –inkey certfile.key –out certfile.pfx

See more OpenSSL Examples

How to create a PKCS12 (.p12 or .pfx) from a certificate file and private key file: Demonstrates how to duplicate this OpenSSL command:
Duplicate openssl pkcs12 –export –in certfile.cer –inkey certfile.key –out certfile.pfx

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const pkey = try chilkat.PrivateKey.init();
    defer pkey.deinit();

    // Load the private key from the file.
    pkey.loadAnyFormatFile("certFile.key", "") catch {
        std.debug.print("{s}\n", .{try pkey.getLastErrorText(alloc)});
        return;
    };

    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    // The LoadFromFile method auto-recognizes the format...
    cert.loadFromFile("certfile.cer") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    // We'll need a cert chain object to create the PKCS12, so get it
    // from the cert.
ERROR: Method not in the Zig package: Cert.GetCertChain
ERROR: Method not in the Zig package: Cert.GetCertChain
    const cert_chain = cert.ERROR();
    if (!cert.getLastMethodSuccess()) {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    }

    // Create the PFX object, add the cert and private key, and write to a .pfx file.
    const pfx = try chilkat.Pfx.init();
    defer pfx.deinit();

    // The cert(s) are automatically added in the call to AddPrivateKey
    pfx.addPrivateKey(pkey, cert_chain) catch {
        std.debug.print("{s}\n", .{try pfx.getLastErrorText(alloc)});
        return;
    };

    // Write the .pfx to a file.
    const password = "myPassword";
    pfx.toFile(password, "certfile.pfx") catch {
        std.debug.print("{s}\n", .{try pfx.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Success.\n", .{});
}