質問編集履歴

2

サンプルコードと出力例を追記

2016/02/03 19:04

投稿

shinjism
shinjism

スコア11

test CHANGED
File without changes
test CHANGED
@@ -29,3 +29,263 @@
29
29
  Unity 5.2.3f1
30
30
 
31
31
  ※Windows PCアプリとXAMPP環境は同一PC内に共存
32
+
33
+
34
+
35
+ 送信側サンプルコード
36
+
37
+ ```C#
38
+
39
+ private IEnumerator PostWithBinary2(byte[] byteData, System.Action<WWW> callback)
40
+
41
+ {
42
+
43
+ string url = "http://localhost/report/sample.php";
44
+
45
+
46
+
47
+ WWWForm form = new WWWForm();
48
+
49
+ form.AddField("title", "日本語タイトル");
50
+
51
+ form.AddField("date", "2016-01-01");
52
+
53
+ form.AddField("copyright", "Copyright (C) 2016 日本語著作者. All Rights Reserved");
54
+
55
+ form.AddBinaryData("picture", byteData, "picture.png", "image/png");
56
+
57
+
58
+
59
+ WWW www = new WWW(url, form);
60
+
61
+ yield return www;
62
+
63
+
64
+
65
+ if (callback != null)
66
+
67
+ {
68
+
69
+ callback(www);
70
+
71
+ }
72
+
73
+ }
74
+
75
+ ```
76
+
77
+ 受信側サンプルコード
78
+
79
+ ```PHP
80
+
81
+ <?php
82
+
83
+ ini_set('memory_limit','128M');
84
+
85
+ require_once('./tcpdf/tcpdf.php');
86
+
87
+
88
+
89
+ // Extend the TCPDF class to create custom Header and Footer
90
+
91
+ class MYPDF extends TCPDF
92
+
93
+ {
94
+
95
+ private $hTitle='';
96
+
97
+ private $hDate='';
98
+
99
+ private $fCopyright='';
100
+
101
+
102
+
103
+ //Constructor
104
+
105
+ function __construct($orientation,$unit,$format,$unicode,$encoding,$diskcache,$pdfa,$title,$date,$copyright)
106
+
107
+ {
108
+
109
+ parent::__construct($orientation,$unit,$format,$unicode,$encoding,$diskcache,$pdfa);
110
+
111
+ $this->hTitle=$title;
112
+
113
+ $this->hDate=$date;
114
+
115
+ $this->fCopyright=$copyright;
116
+
117
+ }
118
+
119
+
120
+
121
+ //Page header
122
+
123
+ public function Header() {
124
+
125
+ // Logo
126
+
127
+ $imageFile=dirname(__FILE__).'/logo.jpg';
128
+
129
+ if(file_exists($imageFile))
130
+
131
+ {
132
+
133
+ $this->Image($imageFile,10,15,32,0,'JPG','','T',false,150,'',false,false,0,false,false,false);
134
+
135
+ }
136
+
137
+ // Title
138
+
139
+ $this->SetXY(50,22.5);
140
+
141
+ $this->SetFont('cid0jp','B',16);
142
+
143
+ $this->Cell(110,20,$this->hTitle,0,false,'C',0,'',0,false,'L','M');
144
+
145
+ // Date
146
+
147
+ $this->SetX(190);
148
+
149
+ $this->SetFont('cid0jp','',14);
150
+
151
+ $this->Cell(0,20,$this->hDate,0,false,'R',0,'',0,false,'L','M');
152
+
153
+ }
154
+
155
+
156
+
157
+ // Page footer
158
+
159
+ public function Footer() {
160
+
161
+ // Copyright
162
+
163
+ $this->SetY(-15);
164
+
165
+ $this->SetFont('cid0jp','',10);
166
+
167
+ $this->Cell(0,10,$this->fCopyright,0,false,'C',0,'',0,false,'T','M');
168
+
169
+ }
170
+
171
+ }
172
+
173
+
174
+
175
+ // check Image
176
+
177
+ function checkImage($file)
178
+
179
+ {
180
+
181
+ if($file['type']!='image/png')
182
+
183
+ {
184
+
185
+ return false;
186
+
187
+ }
188
+
189
+ if($file['error']>0)
190
+
191
+ {
192
+
193
+ return false;
194
+
195
+ }
196
+
197
+ if(!move_uploaded_file($file['tmp_name'],dirname(__FILE__).'/images/'.$file['name']))
198
+
199
+ {
200
+
201
+ return false;
202
+
203
+ }
204
+
205
+ return true;
206
+
207
+ }
208
+
209
+
210
+
211
+ // create new PDF document
212
+
213
+ $pdf=new MYPDF(PDF_PAGE_ORIENTATION,PDF_UNIT,PDF_PAGE_FORMAT,true,'UTF-8',false,false,$_POST['title'],$_POST['date'],$_POST['copyright']);
214
+
215
+ $pdf->setHeaderData(PDF_HEADER_LOGO,PDF_HEADER_LOGO_WIDTH,PDF_HEADER_TITLE,PDF_HEADER_STRING);
216
+
217
+ $pdf->setHeaderFont(Array('cid0jp','',14));
218
+
219
+ $pdf->AddPage();
220
+
221
+ $pdf->SetFont('cid0jp','',14);
222
+
223
+ $pdf->SetFillColor(230);
224
+
225
+
226
+
227
+ // output Image
228
+
229
+ if(array_key_exists('picture',$_FILES))
230
+
231
+ {
232
+
233
+ $file=$_FILES['picture'];
234
+
235
+ if (checkImage($file))
236
+
237
+ {
238
+
239
+ $x=10;
240
+
241
+ $y=125;
242
+
243
+ $pdf->Image(dirname(__FILE__).'/images/'.$file['name'],$x,$y,92,0,'PNG','','',true,200,'',false,false,1,false,false,false);
244
+
245
+ }
246
+
247
+ }
248
+
249
+
250
+
251
+ // save PDF
252
+
253
+ $reportFile=dirname(__FILE__).'/report.pdf';
254
+
255
+ if(file_exists($reportFile))
256
+
257
+ {
258
+
259
+ unlink($reportFile);
260
+
261
+ }
262
+
263
+ $pdf->Output($reportFile,'F');
264
+
265
+
266
+
267
+ // return JSON
268
+
269
+ $json = array(
270
+
271
+ 'resultReportFile'=>(empty($_SERVER['HTTPS'])?'http://':'https://').$_SERVER['HTTP_HOST'].(dirname($_SERVER['SCRIPT_NAME']).'/report.pdf'),
272
+
273
+ 'error'=>'',
274
+
275
+ );
276
+
277
+ header('Content-Type: application/json');
278
+
279
+ echo json_encode($json);
280
+
281
+ ?>
282
+
283
+ ```
284
+
285
+ 文字化けしなかった例
286
+
287
+ ![ヘッダー部とフッター部に日本語テキスト出力。左上の青い箇所はPHPスクリプトと同じ場所にある画像を出力。](4d8f47ae413c70b00db69ad37ee7b2f5.png)
288
+
289
+ 文字化けした例
290
+
291
+ ![ヘッダー部とフッター部に日本語テキスト出力。中央の画像がpostしたもの。](7ce6d25783a51d4e9ef0ae446d81e9c5.png)

1

postする画像はバイナリデータである旨を追記しました。

2016/02/03 19:04

投稿

shinjism
shinjism

スコア11

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- Unityで作成したWindows PCアプリから、PDF生成を行うPHPスクリプトに対して日本語テキストと100KB未満の画像をpostし、PDF出力を行っています。
1
+ Unityで作成したWindows PCアプリから、PDF生成を行うPHPスクリプトに対して日本語テキストと100KB未満の画像(バイナリデータ)をpostし、PDF出力を行っています。
2
2
 
3
3
 
4
4