pdf.dartを使って、領収書のPDFを生成して、表示&ダウンロードを試みています。
その際に、PDFの中に日本語を入れたいのですが、
invalid argument(string): Contains invaild characters "領収書"
とエラーが出てきて、日本語を表示させるのに苦戦しています。
以下コードです。
dart
1import 'package:flutter/material.dart'; 2import 'package:pdf/pdf.dart'; 3import 'package:pdf/widgets.dart' as pw; 4import 'package:universal_html/html.dart' as html; 5 6 7class Receipt extends StatelessWidget { 8 final title = '領収書'; 9 10 11 Widget build(BuildContext context) { 12 13 final pdf = pw.Document(); 14 pdf.addPage(pw.Page( 15 pageFormat: PdfPageFormat.a4, 16 build: (pw.Context context) { 17 return pw.Center( 18 child: pw.Container( 19 width: double.infinity, 20 margin: pw.EdgeInsets.all(20.0), 21 padding: pw.EdgeInsets.all(20.0), 22 child: pw.Column(children: [ 23 pw.Text( 24 'RECIEPT', 25 style: pw.TextStyle( 26 fontSize: 32.0, 27 fontWeight: pw.FontWeight.bold, 28 // color: pw.Colors.black87 29 ), 30 ), 31 pw.SizedBox(height: 20.0), 32 pw.Text( 33 title, 34 style: pw.TextStyle( 35 // locale: Locale('ja'), 36 font: , 37 letterSpacing: 1.5, 38 fontSize: 14.0, 39 fontWeight: pw.FontWeight.bold, 40 // color: myTheme.primaryColor 41 ), 42 ), 43 pw.SizedBox(height: 20.0), 44 pw.Align( 45 alignment: pw.Alignment.centerRight, 46 child: pw.Container( 47 width: 120.0, 48 // alignment: pw.AlignmentDirectional.centerEnd, 49 child: pw.Column( 50 crossAxisAlignment: pw.CrossAxisAlignment.end, 51 children: [ 52 pw.Row( 53 mainAxisAlignment: 54 pw.MainAxisAlignment.spaceBetween, 55 children: [ 56 pw.Text('No.'), 57 pw.Text('0000'), 58 ], 59 ), 60 pw.Divider( 61 height: 1.0, 62 thickness: 1.5, 63 // color: myTheme.primaryColor, 64 ), 65 pw.SizedBox(height: 8.0), 66 pw.Row( 67 mainAxisAlignment: 68 pw.MainAxisAlignment.spaceBetween, 69 children: [ 70 pw.Text('日時'), 71 pw.Text('2020/12/11'), 72 ], 73 ), 74 pw.Divider( 75 height: 1.0, 76 thickness: 1.5, 77 // color: myTheme.primaryColor, 78 ), 79 ], 80 ), 81 ), 82 ), 83 pw.SizedBox(height: 20.0), 84 pw.Row( 85 mainAxisAlignment: pw.MainAxisAlignment.spaceBetween, 86 children: [ 87 pw.Text( 88 '名前', 89 style: pw.TextStyle( 90 fontWeight: pw.FontWeight.bold, fontSize: 16.0), 91 ), 92 pw.Text('様') 93 ], 94 ), 95 pw.Divider( 96 thickness: 1.5, 97 // color: myTheme.primaryColor, 98 ), 99 pw.Container( 100 width: double.infinity, 101 padding: pw.EdgeInsets.symmetric( 102 horizontal: 20.0, vertical: 30.0), 103 // color: Colors.pink[50], 104 child: pw.Text( 105 '11,111円', 106 textAlign: pw.TextAlign.center, 107 style: pw.TextStyle( 108 fontSize: 16.0, fontWeight: pw.FontWeight.bold), 109 ), 110 ), 111 pw.SizedBox(height: 16.0), 112 pw.Row( 113 mainAxisAlignment: pw.MainAxisAlignment.spaceBetween, 114 children: [ 115 pw.Text('ただし', 116 style: 117 pw.TextStyle(fontWeight: pw.FontWeight.bold)), 118 pw.Text('チャージ', 119 style: 120 pw.TextStyle(fontWeight: pw.FontWeight.bold)), 121 pw.Text('として', 122 style: 123 pw.TextStyle(fontWeight: pw.FontWeight.bold)), 124 ], 125 ), 126 pw.Divider( 127 height: 4.0, 128 thickness: 1.5, 129 // color: myTheme.primaryColor, 130 ), 131 pw.SizedBox(height: 4.0), 132 pw.Container( 133 alignment: pw.Alignment.centerLeft, 134 child: pw.Text('ご利用代金として領収いたしました。', 135 style: pw.TextStyle(fontWeight: pw.FontWeight.bold)), 136 ), 137 pw.SizedBox(height: 24.0), 138 pw.Row( 139 mainAxisAlignment: pw.MainAxisAlignment.spaceBetween, 140 children: [ 141 pw.Column( 142 crossAxisAlignment: pw.CrossAxisAlignment.start, 143 children: [ 144 pw.Text('会社名', 145 style: pw.TextStyle( 146 fontWeight: pw.FontWeight.bold)), 147 pw.Text('郵便番号'), 148 pw.Text('住所') 149 ], 150 ), 151 pw.Container( 152 width: 80.0, 153 height: 80.0, 154 child: pw.Text('ハンコ'), 155 // color: Colors.grey[200], 156 ) 157 ], 158 ) 159 ]))); 160 } 161 ) 162 ); 163 final bytes = pdf.save(); 164 final blob = html.Blob([bytes], 'application/pdf'); 165 166 return Scaffold( 167 appBar: AppBar(), 168 body: Center( 169 child: Column( 170 children: <Widget>[ 171 RaisedButton( 172 child: Text("Open"), 173 onPressed: () { 174 final url = html.Url.createObjectUrlFromBlob(blob); 175 html.window.open(url, "_blank"); 176 html.Url.revokeObjectUrl(url); 177 }, 178 ), 179 RaisedButton( 180 child: Text("Download"), 181 onPressed: () { 182 final url = html.Url.createObjectUrlFromBlob(blob); 183 final anchor = 184 html.document.createElement('a') as html.AnchorElement 185 ..href = url 186 ..style.display = 'none' 187 ..download = 'some_name.pdf'; 188 html.document.body.children.add(anchor); 189 anchor.click(); 190 html.document.body.children.remove(anchor); 191 html.Url.revokeObjectUrl(url); 192 }, 193 ), 194 ], 195 mainAxisAlignment: MainAxisAlignment.center, 196 ), 197 ), 198 ); 199 } 200} 201```このコード内に使われている日本語のTextが、エラーになります。 202試したことは、pw.DefaultTextStyleを使用してみたり(同じエラーが出て効果無し)
pw.DefaultTextStyle(
child: pw.Text(
title,
),
style: pw.TextStyle(
letterSpacing: 1.5,
fontSize: 14.0,
fontWeight: pw.FontWeight.bold,
// color: myTheme.primaryColor
),
),
GoogleFontのパッケを使ってみたりしました。
pw.Text(
title,
style: pw.GoogleFonts.lato(),
),
> The name 'GoogleFonts' is being referenced through the prefix 'pw', but it isn't defined in any of the libraries imported using that prefix. Try correcting the prefix or importing the library that defines 'GoogleFonts'
あなたの回答
tips
プレビュー