img2pdf
is a simple rust library for converting an image into a PDF file. It supports handling image files with transparency and compressing them into PDF documents. It's really simple.
To convert an image provided as a byte array to a PDF in memory:
use img2pdf::img2pdf_from_bytes;
let image_data = std::fs::read("sample_image.jpg").expect("Failed to read image");
let pdf_data = img2pdf_from_bytes(&image_data).expect("Failed to convert image to PDF");
std::fs::write("output.pdf", pdf_data).expect("Failed to write PDF file");
To convert an image file directly to a PDF file:
use img2pdf::img2pdf_file;
img2pdf_file("sample_image.png", "output.pdf").expect("Failed to convert image to PDF");
There is also a CLI option, but it may not be relevant for your use case
img2pdf <input_image> <output_pdf>