@@ -26,7 +26,7 @@ let mut options = svg2pdf::usvg::Options::default();
2626options.fontdb_mut().load_system_fonts();
2727let tree = svg2pdf::usvg::Tree::from_str(&svg, &options)?;
2828
29- let pdf = svg2pdf::to_pdf(&tree, ConversionOptions::default(), PageOptions::default());
29+ let pdf = svg2pdf::to_pdf(&tree, ConversionOptions::default(), PageOptions::default()).unwrap() ;
3030std::fs::write(output, pdf)?;
3131# Ok(()) }
3232```
@@ -56,8 +56,11 @@ comprehensive list.
5656mod render;
5757mod util;
5858
59+ use std:: fmt;
60+ use std:: fmt:: { Display , Formatter } ;
5961pub use usvg;
6062
63+ use crate :: ConversionError :: UnknownError ;
6164use once_cell:: sync:: Lazy ;
6265use pdf_writer:: { Chunk , Content , Filter , Finish , Pdf , Ref , TextStr } ;
6366use usvg:: { Size , Transform , Tree } ;
@@ -88,6 +91,38 @@ impl Default for PageOptions {
8891 }
8992}
9093
94+ /// A error that can appear during conversion.
95+ #[ derive( Copy , Clone , Debug ) ]
96+ pub enum ConversionError {
97+ /// The SVG image contains an unrecognized type of image.
98+ InvalidImage ,
99+ /// An unknown error occurred during the conversion. This could indicate a bug in the
100+ /// svg2pdf.
101+ UnknownError ,
102+ /// An error occurred while subsetting a font.
103+ #[ cfg( feature = "text" ) ]
104+ SubsetError ( fontdb:: ID ) ,
105+ /// An error occurred while reading a font.
106+ #[ cfg( feature = "text" ) ]
107+ InvalidFont ( fontdb:: ID ) ,
108+ }
109+
110+ impl Display for ConversionError {
111+ fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
112+ match self {
113+ Self :: InvalidImage => f. write_str ( "An unknown type of image appears in the SVG." ) ,
114+ Self :: UnknownError => f. write_str ( "An unknown error occurred during the conversion. This could indicate a bug in svg2pdf" ) ,
115+ #[ cfg( feature = "text" ) ]
116+ Self :: SubsetError ( _) => f. write_str ( "An error occurred while subsetting a font." ) ,
117+ #[ cfg( feature = "text" ) ]
118+ Self :: InvalidFont ( _) => f. write_str ( "An error occurred while reading a font." ) ,
119+ }
120+ }
121+ }
122+
123+ /// The result type for everything.
124+ type Result < T > = std:: result:: Result < T , ConversionError > ;
125+
91126/// Options for the PDF conversion.
92127#[ derive( Copy , Clone ) ]
93128pub struct ConversionOptions {
@@ -146,23 +181,23 @@ impl Default for ConversionOptions {
146181/// let mut tree = svg2pdf::usvg::Tree::from_str(&svg, &options)?;
147182///
148183///
149- /// let pdf = svg2pdf::to_pdf(&tree, ConversionOptions::default(), PageOptions::default());
184+ /// let pdf = svg2pdf::to_pdf(&tree, ConversionOptions::default(), PageOptions::default()).unwrap() ;
150185/// std::fs::write(output, pdf)?;
151186/// # Ok(()) }
152187/// ```
153188pub fn to_pdf (
154189 tree : & Tree ,
155190 conversion_options : ConversionOptions ,
156191 page_options : PageOptions ,
157- ) -> Vec < u8 > {
192+ ) -> Result < Vec < u8 > > {
158193 let mut ctx = Context :: new ( tree, conversion_options) ;
159194 let mut pdf = Pdf :: new ( ) ;
160195
161196 let dpi_ratio = 72.0 / page_options. dpi ;
162197 let dpi_transform = Transform :: from_scale ( dpi_ratio, dpi_ratio) ;
163198 let page_size =
164199 Size :: from_wh ( tree. size ( ) . width ( ) * dpi_ratio, tree. size ( ) . height ( ) * dpi_ratio)
165- . unwrap ( ) ;
200+ . ok_or ( UnknownError ) ? ;
166201
167202 let catalog_ref = ctx. alloc_ref ( ) ;
168203 let page_tree_ref = ctx. alloc_ref ( ) ;
@@ -177,7 +212,7 @@ pub fn to_pdf(
177212 let mut content = Content :: new ( ) ;
178213 content. save_state ( ) ;
179214 content. transform ( dpi_transform. to_pdf_transform ( ) ) ;
180- tree_to_stream ( tree, & mut pdf, & mut content, & mut ctx, & mut rc) ;
215+ tree_to_stream ( tree, & mut pdf, & mut content, & mut ctx, & mut rc) ? ;
181216 content. restore_state ( ) ;
182217 let content_stream = ctx. finish_content ( content) ;
183218 let mut stream = pdf. stream ( content_ref, & content_stream) ;
@@ -203,12 +238,12 @@ pub fn to_pdf(
203238 page. contents ( content_ref) ;
204239 page. finish ( ) ;
205240
206- ctx. write_global_objects ( & mut pdf) ;
241+ ctx. write_global_objects ( & mut pdf) ? ;
207242
208243 let document_info_id = ctx. alloc_ref ( ) ;
209244 pdf. document_info ( document_info_id) . producer ( TextStr ( "svg2pdf" ) ) ;
210245
211- pdf. finish ( )
246+ Ok ( pdf. finish ( ) )
212247}
213248
214249/// Convert a [Tree] into a [`Chunk`].
@@ -251,7 +286,7 @@ pub fn to_pdf(
251286/// let mut options = svg2pdf::usvg::Options::default();
252287/// options.fontdb_mut().load_system_fonts();
253288/// let tree = svg2pdf::usvg::Tree::from_str(&svg, &options)?;
254- /// let (mut svg_chunk, svg_id) = svg2pdf::to_chunk(&tree, svg2pdf::ConversionOptions::default());
289+ /// let (mut svg_chunk, svg_id) = svg2pdf::to_chunk(&tree, svg2pdf::ConversionOptions::default()).unwrap() ;
255290///
256291/// // Renumber the chunk so that we can embed it into our existing workflow, and also make sure
257292/// // to update `svg_id`.
@@ -306,11 +341,14 @@ pub fn to_pdf(
306341/// std::fs::write("target/embedded.pdf", pdf.finish())?;
307342/// # Ok(()) }
308343/// ```
309- pub fn to_chunk ( tree : & Tree , conversion_options : ConversionOptions ) -> ( Chunk , Ref ) {
344+ pub fn to_chunk (
345+ tree : & Tree ,
346+ conversion_options : ConversionOptions ,
347+ ) -> Result < ( Chunk , Ref ) > {
310348 let mut chunk = Chunk :: new ( ) ;
311349
312350 let mut ctx = Context :: new ( tree, conversion_options) ;
313- let x_ref = tree_to_xobject ( tree, & mut chunk, & mut ctx) ;
314- ctx. write_global_objects ( & mut chunk) ;
315- ( chunk, x_ref)
351+ let x_ref = tree_to_xobject ( tree, & mut chunk, & mut ctx) ? ;
352+ ctx. write_global_objects ( & mut chunk) ? ;
353+ Ok ( ( chunk, x_ref) )
316354}
0 commit comments