Image to PDF conversion, substantially

This commit is contained in:
John Whitington 2024-09-30 18:24:10 +01:00
parent 8a9eeb6933
commit 4f0346c615
2 changed files with 13 additions and 6 deletions

View File

@ -774,7 +774,7 @@ let write_structure_tree pdf st =
("/K", Pdf.Array items)]) ("/K", Pdf.Array items)])
in in
Pdf.addobj_given_num pdf (struct_tree_root, st); Pdf.addobj_given_num pdf (struct_tree_root, st);
Pdf.replace_chain pdf ["/Root"] ("/StructTreeRoot", (Pdf.Indirect struct_tree_root)) Pdf.replace_chain pdf ["/Root"] ("/StructTreeRoot", Pdf.Indirect struct_tree_root)
let draw ~struct_tree ~fast ~underneath ~filename ~bates ~batespad range pdf drawops = let draw ~struct_tree ~fast ~underneath ~filename ~bates ~batespad range pdf drawops =
(*Printf.printf "%s\n" (string_of_drawops drawops);*) (*Printf.printf "%s\n" (string_of_drawops drawops);*)

View File

@ -494,18 +494,18 @@ let obj_of_jbig2_data ?global data =
Pdf.Stream {contents = (Pdf.Dictionary d, Pdf.Got data)}, extra Pdf.Stream {contents = (Pdf.Dictionary d, Pdf.Got data)}, extra
let image_of_input ?subformat ?title fobj i = let image_of_input ?subformat ?title fobj i =
let pdf = let pdf, title =
match subformat with match subformat with
| None -> Pdf.empty () | None -> Pdf.empty (), begin match title with Some x -> x | None -> "" end
| Some Cpdfua.PDFUA1 -> | Some Cpdfua.PDFUA1 ->
begin match title with begin match title with
| None -> error "no -title given" | None -> error "no -title given"
| Some title -> Cpdfua.create_pdfua1 title Pdfpaper.a4 1 | Some title -> Cpdfua.create_pdfua1 title Pdfpaper.a4 1, title
end end
| Some Cpdfua.PDFUA2 -> | Some Cpdfua.PDFUA2 ->
begin match title with begin match title with
| None -> error "no -title given" | None -> error "no -title given"
| Some title -> Cpdfua.create_pdfua2 title Pdfpaper.a4 1 | Some title -> Cpdfua.create_pdfua2 title Pdfpaper.a4 1, title
end end
in in
let data = Pdfio.bytes_of_input i 0 i.Pdfio.in_channel_length in let data = Pdfio.bytes_of_input i 0 i.Pdfio.in_channel_length in
@ -513,6 +513,13 @@ let image_of_input ?subformat ?title fobj i =
iter (Pdf.addobj_given_num pdf) extras; iter (Pdf.addobj_given_num pdf) extras;
let w = match Pdf.lookup_direct pdf "/Width" obj with Some x -> Pdf.getnum pdf x | _ -> assert false in let w = match Pdf.lookup_direct pdf "/Width" obj with Some x -> Pdf.getnum pdf x | _ -> assert false in
let h = match Pdf.lookup_direct pdf "/Height" obj with Some x -> Pdf.getnum pdf x | _ -> assert false in let h = match Pdf.lookup_direct pdf "/Height" obj with Some x -> Pdf.getnum pdf x | _ -> assert false in
let str = Pdf.addobj pdf Pdf.Null in
let figure = Pdf.addobj pdf Pdf.Null in
let parent_tree = Pdf.addobj pdf Pdf.Null in
Pdf.addobj_given_num pdf (parent_tree, Pdf.Dictionary [("/Nums", Pdf.Array [Pdf.Integer 1; Pdf.Array [Pdf.Indirect figure]])]);
Pdf.addobj_given_num pdf (figure, Pdf.Dictionary [("/K", Pdf.Array [Pdf.Integer 0]); ("/P", Pdf.Indirect str); ("/S", Pdf.Name "/Figure"); ("/Alt", Pdf.String title)]);
Pdf.addobj_given_num pdf (str, Pdf.Dictionary [("/Type", Pdf.Name "/StructTreeRoot"); ("/K", Pdf.Array [Pdf.Indirect figure]); ("/ParentTree", Pdf.Indirect parent_tree)]);
Pdf.replace_chain pdf ["/Root"] ("/StructTreeRoot", Pdf.Indirect str);
let page = let page =
{Pdfpage.content = {Pdfpage.content =
[Pdfops.stream_of_ops [Pdfops.stream_of_ops
@ -526,7 +533,7 @@ let image_of_input ?subformat ?title fobj i =
Pdf.Dictionary Pdf.Dictionary
["/XObject", Pdf.Dictionary ["/I0", Pdf.Indirect (Pdf.addobj pdf obj)]]; ["/XObject", Pdf.Dictionary ["/I0", Pdf.Indirect (Pdf.addobj pdf obj)]];
Pdfpage.rotate = Pdfpage.Rotate0; Pdfpage.rotate = Pdfpage.Rotate0;
Pdfpage.rest = Pdf.Dictionary []} Pdfpage.rest = Pdf.Dictionary [("/StructTreeParents", Pdf.Integer 1)]}
in in
let pdf, pageroot = Pdfpage.add_pagetree [page] pdf in let pdf, pageroot = Pdfpage.add_pagetree [page] pdf in
Pdfpage.add_root pageroot [] pdf Pdfpage.add_root pageroot [] pdf