@@ -14,6 +14,10 @@ defmodule Mudbrick.Parser do
14
14
TJ
15
15
}
16
16
17
+ defmodule Error do
18
+ defexception [ :message ]
19
+ end
20
+
17
21
@ doc """
18
22
Parse Mudbrick-generated `iodata` into a `Mudbrick.Document`.
19
23
@@ -27,50 +31,53 @@ defmodule Mudbrick.Parser do
27
31
"""
28
32
@ spec parse ( iodata ( ) ) :: Mudbrick.Document . t ( )
29
33
def parse ( iodata ) do
30
- { :ok , parsed_items , _rest , % { } , _ , _ } =
31
- iodata
32
- |> IO . iodata_to_binary ( )
33
- |> pdf ( )
34
-
35
- items = Enum . flat_map ( parsed_items , & to_mudbrick / 1 )
36
-
37
- page_refs = page_refs ( items )
38
- font_files = Enum . filter ( items , & font? / 1 )
39
- image_files = Enum . filter ( items , & image? / 1 )
40
- fonts = decompressed_resources_option ( font_files , "F" )
41
- images = decompressed_resources_option ( image_files , "I" )
42
- compress? = Enum . any? ( items , & match? ( % { value: % { compress: true } } , & 1 ) )
43
-
44
- opts =
45
- [ compress: compress? ] ++
46
- case Enum . find ( items , & metadata? / 1 ) do
47
- nil ->
48
- [ ]
49
-
50
- metadata ->
51
- xml =
52
- if metadata . value . compress do
53
- Mudbrick . decompress ( metadata . value . data )
54
- else
55
- metadata . value . data
56
- end
57
-
58
- metadata ( xml )
34
+ input = IO . iodata_to_binary ( iodata )
35
+
36
+ case pdf ( input ) do
37
+ { :error , msg , rest , % { } , _things , _bytes } ->
38
+ raise Error , "#{ msg } \n #{ rest } "
39
+
40
+ { :ok , parsed_items , _rest , % { } , _ , _ } ->
41
+ items = Enum . flat_map ( parsed_items , & to_mudbrick / 1 )
42
+
43
+ page_refs = page_refs ( items )
44
+ font_files = Enum . filter ( items , & font? / 1 )
45
+ image_files = Enum . filter ( items , & image? / 1 )
46
+ fonts = decompressed_resources_option ( font_files , "F" )
47
+ images = decompressed_resources_option ( image_files , "I" )
48
+ compress? = Enum . any? ( items , & match? ( % { value: % { compress: true } } , & 1 ) )
49
+
50
+ opts =
51
+ [ compress: compress? ] ++
52
+ case Enum . find ( items , & metadata? / 1 ) do
53
+ nil ->
54
+ [ ]
55
+
56
+ metadata ->
57
+ xml =
58
+ if metadata . value . compress do
59
+ Mudbrick . decompress ( metadata . value . data )
60
+ else
61
+ metadata . value . data
62
+ end
63
+
64
+ metadata ( xml )
65
+ end
66
+
67
+ opts = if map_size ( fonts ) > 0 , do: Keyword . put ( opts , :fonts , fonts ) , else: opts
68
+ opts = if map_size ( images ) > 0 , do: Keyword . put ( opts , :images , images ) , else: opts
69
+
70
+ for page <- all ( items , page_refs ) , reduce: Mudbrick . new ( opts ) do
71
+ acc ->
72
+ [ _ , _ , w , h ] = page . value [ :MediaBox ]
73
+
74
+ Mudbrick . page ( acc , size: { w , h } )
75
+ |> Mudbrick.ContentStream . update_operations ( fn ops ->
76
+ operations ( items , page ) ++ ops
77
+ end )
59
78
end
60
-
61
- opts = if map_size ( fonts ) > 0 , do: Keyword . put ( opts , :fonts , fonts ) , else: opts
62
- opts = if map_size ( images ) > 0 , do: Keyword . put ( opts , :images , images ) , else: opts
63
-
64
- for page <- all ( items , page_refs ) , reduce: Mudbrick . new ( opts ) do
65
- acc ->
66
- [ _ , _ , w , h ] = page . value [ :MediaBox ]
67
-
68
- Mudbrick . page ( acc , size: { w , h } )
69
- |> Mudbrick.ContentStream . update_operations ( fn ops ->
70
- operations ( items , page ) ++ ops
71
- end )
79
+ |> Mudbrick.Document . finish ( )
72
80
end
73
- |> Mudbrick.Document . finish ( )
74
81
end
75
82
76
83
@ doc """
0 commit comments