From 65fec4d2758d9b16456c9e5005fc13e79a3c453e Mon Sep 17 00:00:00 2001 From: John Whitington Date: Mon, 6 May 2024 15:37:58 +0800 Subject: [PATCH] Allow typesetting of the empty document --- cpdftexttopdf.ml | 7 +++++-- cpdftype.ml | 17 ++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/cpdftexttopdf.ml b/cpdftexttopdf.ml index e456704..0424084 100644 --- a/cpdftexttopdf.ml +++ b/cpdftexttopdf.ml @@ -56,8 +56,11 @@ let typeset ~papersize ~font ~fontsize text = in let instrs = of_utf8_with_newlines fontpack fontsize (Pdfio.string_of_bytes text) in let margin = Pdfunits.points (Pdfpaper.width papersize) (Pdfpaper.unit papersize) /. 15. in - let firstfont = hd (keep (function Cpdftype.Font _ -> true | _ -> false) instrs) in - let instrs = [firstfont; Cpdftype.BeginDocument] @ instrs in + let instrs = + if instrs = [] then [] else + let firstfont = hd (keep (function Cpdftype.Font _ -> true | _ -> false) instrs) in + [firstfont; Cpdftype.BeginDocument] @ instrs + in let pages = Cpdftype.typeset margin margin margin margin papersize pdf instrs in let pdf, pageroot = Pdfpage.add_pagetree pages pdf in Pdfpage.add_root pageroot [] pdf diff --git a/cpdftype.ml b/cpdftype.ml index c1a4b47..8812a8b 100644 --- a/cpdftype.ml +++ b/cpdftype.ml @@ -208,15 +208,14 @@ let typeset lmargin rmargin tmargin bmargin papersize pdf i = let thisdestrectangles = ref [] in let pages = ref [] in let write_page () = - if !ops <> [] then - let page = - {Pdfpage.content = [Pdfops.stream_of_ops (rev !ops)]; - Pdfpage.mediabox = Pdfpage.rectangle_of_paper papersize; - Pdfpage.resources = make_resources !thispagefontnums; - Pdfpage.rotate = Pdfpage.Rotate0; - Pdfpage.rest = make_annotations pdf !thispageannotations} - in - pages := page :: !pages + let page = + {Pdfpage.content = if !ops = [] then [] else [Pdfops.stream_of_ops (rev !ops)]; + Pdfpage.mediabox = Pdfpage.rectangle_of_paper papersize; + Pdfpage.resources = make_resources !thispagefontnums; + Pdfpage.rotate = Pdfpage.Rotate0; + Pdfpage.rest = make_annotations pdf !thispageannotations} + in + pages := page :: !pages in let rec typeset_element = function | Text cps ->