pub struct ImageLoader {
base_dir: Option<PathBuf>,
cache: HashMap<String, ImageData>,
allow_remote: bool,
}Expand description
Manages loading and caching of images for a document.
Provides automatic path resolution for relative paths and handles both local filesystem and remote HTTP(S) image loading.
Fields§
§base_dir: Option<PathBuf>Base directory for resolving relative paths
cache: HashMap<String, ImageData>Cache of loaded images to avoid redownloading
allow_remote: boolWhether to enable remote image downloading
Implementations§
Source§impl ImageLoader
impl ImageLoader
Sourcepub fn new(document_path: Option<&Path>) -> Self
pub fn new(document_path: Option<&Path>) -> Self
Create a new image loader for a document at the specified path.
The base directory is extracted from the document path and used to resolve relative image references.
§Arguments
document_path- Path to the markdown document (optional)
§Example
use markdown2pdf::images::ImageLoader;
use std::path::Path;
let loader = ImageLoader::new(Some(Path::new("./docs/document.md")));
// Images will be resolved relative to ./docs/Sourcepub fn set_allow_remote(&mut self, allow: bool)
pub fn set_allow_remote(&mut self, allow: bool)
Enable or disable remote image downloading.
§Example
use markdown2pdf::images::ImageLoader;
let mut loader = ImageLoader::new(None);
loader.set_allow_remote(false);
// Will not download images from URLsSourcepub fn resolve_path(&self, url_or_path: &str) -> Result<String, ImageError>
pub fn resolve_path(&self, url_or_path: &str) -> Result<String, ImageError>
Resolve an image path relative to the document location.
For absolute URLs (http/https), returns the URL as-is. For relative paths, resolves them relative to the document directory.
§Arguments
url_or_path- Image URL or relative path
§Returns
Either an absolute path string for local images or a full URL for remote images
§Example
use markdown2pdf::images::ImageLoader;
use std::path::Path;
let loader = ImageLoader::new(Some(Path::new("./docs/document.md")));
// Relative path gets resolved
// loader.resolve_path("images/photo.jpg") -> "/full/path/to/docs/images/photo.jpg"
// HTTP URL stays as-is
// loader.resolve_path("https://example.com/image.jpg") -> "https://example.com/image.jpg"Sourcefn load_local(&self, path: &str) -> Result<Vec<u8>, ImageError>
fn load_local(&self, path: &str) -> Result<Vec<u8>, ImageError>
Load an image from the local filesystem.
§Errors
Returns ImageError::LoadError if the file cannot be read.
Sourcefn load_remote(&self, url: &str) -> Result<Vec<u8>, ImageError>
fn load_remote(&self, url: &str) -> Result<Vec<u8>, ImageError>
Download an image from a remote URL.
Requires the fetch feature to be enabled.
§Errors
Returns ImageError::DownloadError if the download fails.
Auto Trait Implementations§
impl Freeze for ImageLoader
impl RefUnwindSafe for ImageLoader
impl Send for ImageLoader
impl Sync for ImageLoader
impl Unpin for ImageLoader
impl UnwindSafe for ImageLoader
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
§fn to_owned_obj(&self, data: FontData<'_>) -> U
fn to_owned_obj(&self, data: FontData<'_>) -> U
T, using the provided data to resolve any offsets.