| 
 | 1 | +import fitz  | 
 | 2 | +import json  | 
 | 3 | + | 
 | 4 | +my_json =  """  | 
 | 5 | +[  | 
 | 6 | +    {  | 
 | 7 | +         "name" :           "Five-storied Pagoda",  | 
 | 8 | +         "temple" :         "Rurikō-ji",  | 
 | 9 | +         "founded" :        "middle Muromachi period, 1442",  | 
 | 10 | +         "region" :         "Yamaguchi, Yamaguchi",  | 
 | 11 | +         "position" :       "34.190181,131.472917"  | 
 | 12 | +     },  | 
 | 13 | +     {  | 
 | 14 | +         "name" :           "Founder's Hall",  | 
 | 15 | +         "temple" :         "Eihō-ji",  | 
 | 16 | +         "founded" :        "early Muromachi period",  | 
 | 17 | +         "region" :         "Tajimi, Gifu",  | 
 | 18 | +         "position" :       "35.346144,137.129189"  | 
 | 19 | +     },  | 
 | 20 | +     {  | 
 | 21 | +         "name" :           "Fudōdō",  | 
 | 22 | +         "temple" :         "Kongōbu-ji",  | 
 | 23 | +         "founded" :        "early Kamakura period",  | 
 | 24 | +         "region" :         "Kōya, Wakayama",  | 
 | 25 | +         "position" :       "34.213103,135.580397"  | 
 | 26 | +     },  | 
 | 27 | +     {  | 
 | 28 | +         "name" :           "Goeidō",  | 
 | 29 | +         "temple" :         "Nishi Honganji",  | 
 | 30 | +         "founded" :        "Edo period, 1636",  | 
 | 31 | +         "region" :         "Kyoto",  | 
 | 32 | +         "position" :       "34.991394,135.751689"  | 
 | 33 | +     },  | 
 | 34 | +     {  | 
 | 35 | +         "name" :           "Golden Hall",  | 
 | 36 | +         "temple" :         "Murō-ji",  | 
 | 37 | +         "founded" :        "early Heian period",  | 
 | 38 | +         "region" :         "Uda, Nara",  | 
 | 39 | +         "position" :       "34.536586819357986,136.0395548452301"  | 
 | 40 | +     },  | 
 | 41 | +     {  | 
 | 42 | +         "name" :           "Golden Hall",  | 
 | 43 | +         "temple" :         "Fudō-in",  | 
 | 44 | +         "founded" :        "late Muromachi period, 1540",  | 
 | 45 | +         "region" :         "Hiroshima",  | 
 | 46 | +         "position" :       "34.427014,132.471117"  | 
 | 47 | +     },  | 
 | 48 | +     {  | 
 | 49 | +         "name" :           "Golden Hall",  | 
 | 50 | +         "temple" :         "Ninna-ji",  | 
 | 51 | +         "founded" :        "Momoyama period, 1613",  | 
 | 52 | +         "region" :         "Kyoto",  | 
 | 53 | +         "position" :       "35.031078,135.713811"  | 
 | 54 | +     },  | 
 | 55 | +     {  | 
 | 56 | +         "name" :           "Golden Hall",  | 
 | 57 | +         "temple" :         "Mii-dera",  | 
 | 58 | +         "founded" :        "Momoyama period, 1599",  | 
 | 59 | +         "region" :         "Ōtsu, Shiga",  | 
 | 60 | +         "position" :       "35.013403,135.852861"  | 
 | 61 | +     },  | 
 | 62 | +     {  | 
 | 63 | +         "name" :           "Golden Hall",  | 
 | 64 | +         "temple" :         "Tōshōdai-ji",  | 
 | 65 | +         "founded" :        "Nara period, 8th century",  | 
 | 66 | +         "region" :         "Nara, Nara",  | 
 | 67 | +         "position" :       "34.675619,135.784842"  | 
 | 68 | +     },  | 
 | 69 | +     {  | 
 | 70 | +         "name" :           "Golden Hall",  | 
 | 71 | +         "temple" :         "Tō-ji",  | 
 | 72 | +         "founded" :        "Momoyama period, 1603",  | 
 | 73 | +         "region" :         "Kyoto",  | 
 | 74 | +         "position" :       "34.980367,135.747686"  | 
 | 75 | +     },  | 
 | 76 | +     {  | 
 | 77 | +         "name" :           "Golden Hall",  | 
 | 78 | +         "temple" :         "Tōdai-ji",  | 
 | 79 | +         "founded" :        "middle Edo period, 1705",  | 
 | 80 | +         "region" :         "Nara, Nara",  | 
 | 81 | +         "position" :       "34.688992,135.839822"  | 
 | 82 | +     },  | 
 | 83 | +     {  | 
 | 84 | +         "name" :           "Golden Hall",  | 
 | 85 | +         "temple" :         "Hōryū-ji",  | 
 | 86 | +         "founded" :        "Asuka period, by 693",  | 
 | 87 | +         "region" :         "Ikaruga, Nara",  | 
 | 88 | +         "position" :       "34.614317,135.734458"  | 
 | 89 | +     },  | 
 | 90 | +     {  | 
 | 91 | +         "name" :           "Golden Hall",  | 
 | 92 | +         "temple" :         "Daigo-ji",  | 
 | 93 | +         "founded" :        "late Heian period",  | 
 | 94 | +         "region" :         "Kyoto",  | 
 | 95 | +         "position" :       "34.951481,135.821747"  | 
 | 96 | +     },  | 
 | 97 | +     {  | 
 | 98 | +         "name" :           "Keigū-in Main Hall",  | 
 | 99 | +         "temple" :         "Kōryū-ji",  | 
 | 100 | +         "founded" :        "early Kamakura period, before 1251",  | 
 | 101 | +         "region" :         "Kyoto",  | 
 | 102 | +         "position" :       "35.015028,135.705425"  | 
 | 103 | +     },  | 
 | 104 | +     {  | 
 | 105 | +         "name" :           "Konpon-chūdō",  | 
 | 106 | +         "temple" :         "Enryaku-ji",  | 
 | 107 | +         "founded" :        "early Edo period, 1640",  | 
 | 108 | +         "region" :         "Ōtsu, Shiga",  | 
 | 109 | +         "position" :       "35.070456,135.840942"  | 
 | 110 | +     },  | 
 | 111 | +     {  | 
 | 112 | +         "name" :           "Korō",  | 
 | 113 | +         "temple" :         "Tōshōdai-ji",  | 
 | 114 | +         "founded" :        "early Kamakura period, 1240",  | 
 | 115 | +         "region" :         "Nara, Nara",  | 
 | 116 | +         "position" :       "34.675847,135.785069"  | 
 | 117 | +     },  | 
 | 118 | +     {  | 
 | 119 | +         "name" :           "Kōfūzō",  | 
 | 120 | +         "temple" :         "Hōryū-ji",  | 
 | 121 | +         "founded" :        "early Heian period",  | 
 | 122 | +         "region" :         "Ikaruga, Nara",  | 
 | 123 | +         "position" :       "34.614439,135.735428"  | 
 | 124 | +     },  | 
 | 125 | +     {  | 
 | 126 | +         "name" :           "Large Lecture Hall",  | 
 | 127 | +         "temple" :         "Hōryū-ji",  | 
 | 128 | +         "founded" :        "middle Heian period, 990",  | 
 | 129 | +         "region" :         "Ikaruga, Nara",  | 
 | 130 | +         "position" :       "34.614783,135.734175"  | 
 | 131 | +     },  | 
 | 132 | +     {  | 
 | 133 | +         "name" :           "Lecture Hall",  | 
 | 134 | +         "temple" :         "Zuiryū-ji",  | 
 | 135 | +         "founded" :        "early Edo period, 1655",  | 
 | 136 | +         "region" :         "Takaoka, Toyama",  | 
 | 137 | +         "position" :       "36.735689,137.010019"  | 
 | 138 | +     },  | 
 | 139 | +     {  | 
 | 140 | +         "name" :           "Lecture Hall",  | 
 | 141 | +         "temple" :         "Tōshōdai-ji",  | 
 | 142 | +         "founded" :        "Nara period, 763",  | 
 | 143 | +         "region" :         "Nara, Nara",  | 
 | 144 | +         "position" :       "34.675933,135.784842"  | 
 | 145 | +     },  | 
 | 146 | +     {  | 
 | 147 | +         "name" :           "Lotus Flower Gate",  | 
 | 148 | +         "temple" :         "Tō-ji",  | 
 | 149 | +         "founded" :        "early Kamakura period",  | 
 | 150 | +         "region" :         "Kyoto",  | 
 | 151 | +         "position" :       "34.980678,135.746314"  | 
 | 152 | +     },  | 
 | 153 | +     {  | 
 | 154 | +         "name" :           "Main Hall",  | 
 | 155 | +         "temple" :         "Akishinodera",  | 
 | 156 | +         "founded" :        "early Kamakura period",  | 
 | 157 | +         "region" :         "Nara, Nara",  | 
 | 158 | +         "position" :       "34.703769,135.776189"  | 
 | 159 | +     }  | 
 | 160 | +]  | 
 | 161 | +
  | 
 | 162 | +"""  | 
 | 163 | + | 
 | 164 | +# the result is a Python dictionary:  | 
 | 165 | +my_dict = json.loads(my_json)  | 
 | 166 | + | 
 | 167 | +MEDIABOX = fitz.paper_rect("letter")  # output page format: Letter  | 
 | 168 | +WHERE = MEDIABOX + (36, 36, -36, -36)  | 
 | 169 | +writer = fitz.DocumentWriter("json-example.pdf")  # create the writer  | 
 | 170 | + | 
 | 171 | +story = fitz.Story()  | 
 | 172 | +body = story.body  | 
 | 173 | + | 
 | 174 | +for i, entry in enumerate(my_dict):  | 
 | 175 | + | 
 | 176 | +    for attribute, value in entry.items():  | 
 | 177 | +        para = body.add_paragraph()  | 
 | 178 | + | 
 | 179 | +        if attribute == "position":  | 
 | 180 | +            para.set_fontsize(10)  | 
 | 181 | +            para.add_link(f"www.google.com/maps/@{value},14z")  | 
 | 182 | +        else:  | 
 | 183 | +            para.add_span()  | 
 | 184 | +            para.set_color("#990000")  | 
 | 185 | +            para.set_fontsize(14)  | 
 | 186 | +            para.set_bold()  | 
 | 187 | +            para.add_text(f"{attribute} ")  | 
 | 188 | +            para.add_span()  | 
 | 189 | +            para.set_fontsize(18)  | 
 | 190 | +            para.add_text(f"{value}")  | 
 | 191 | + | 
 | 192 | +    body.add_horizontal_line()  | 
 | 193 | + | 
 | 194 | +# This while condition will check a value from the Story `place` method  | 
 | 195 | +# for whether all content for the story has been written (0), otherwise  | 
 | 196 | +# more content is waiting to be written (1)  | 
 | 197 | +more = 1  | 
 | 198 | +while more:  | 
 | 199 | +    device = writer.begin_page(MEDIABOX)  # make new page  | 
 | 200 | +    more, _ = story.place(WHERE)  | 
 | 201 | +    story.draw(device)  | 
 | 202 | +    writer.end_page()  # finish page  | 
 | 203 | + | 
 | 204 | +writer.close()  # close output file  | 
 | 205 | + | 
 | 206 | +del story  | 
0 commit comments