1 composer require phpoffice/phpword
2 use PhpOffice\PhpWord\TemplateProcessor;
3
$japan = JapanModel::find($id);
// qrCode
$qrCodePath = root_path('storage/japan/qrcodes') . $id . '.png';
//$qrCode = new QrCode('1,护照号码,有效日期,姓, 名,性别,出生日期,国籍,身份证号码,在日担保人姓名,电话,地址');
$qrCode = new QrCode("1,{$japan->passport_no},{$japan->expiry_date},{$japan->family_name_en}, {$japan->family_name_cn},{$japan->sex},{$japan->date_birth},{$japan->nationality},{$japan->id_card},{$japan->guarantor_en},{$japan->guarantor_tel},{$japan->guarantor_address_en}");
$qrCode->setSize(250);
$qrCode->setMargin(1);
$qrCode->writeFile($qrCodePath);
// Word
$phpWord = new TemplateProcessor(root_path('storage/japan/templates') . 'japan.docx');
$phpWord->setImageValue('qrcode', [
'path' => $qrCodePath,
]);
//性别
if ($japan->sex === '男') {
$sex_male = '☑';
$sex_female = '□';
} else {
$sex_male = '□';
$sex_female = '☑';
};
//婚姻状况
if ($japan->marital === '单身') {
$marital_ds = '☑';
$marital_yh = '□';
$marital_sa = '□';
$marital_lh = '□';
} elseif ($japan->marital === '已婚'){
$marital_ds = '□';
$marital_yh = '☑';
$marital_sa = '□';
$marital_lh = '□';
} elseif ($japan->marital === '丧偶'){
$marital_ds = '□';
$marital_yh = '□';
$marital_sa = '☑';
$marital_lh = '□';
} else {
$marital_ds = '□';
$marital_yh = '□';
$marital_sa = '□';
$marital_lh = '☑';
};
//护照类型
if ($japan->passport_type === '外交') {
$hz_wj = '☑';
$hz_gw = '□';
$hz_pt = '□';
$hz_qt = '□';
} elseif ($japan->passport_type === '公务'){
$hz_wj = '□';
$hz_gw = '☑';
$hz_pt = '□';
$hz_qt = '□';
} elseif ($japan->passport_type === '普通'){
$hz_wj = '□';
$hz_gw = '□';
$hz_pt = '☑';
$hz_qt = '□';
} else {
$hz_wj = '□';
$hz_gw = '□';
$hz_pt = '□';
$hz_qt = '☑';
};
//性别guarantor_sex
if ($japan->guarantor_sex === '男') {
$gsex_m = '☑';
$gsex_f = '□';
} else {
$gsex_m = '□';
$gsex_f = '☑';
};
//性别inviter_sex
if ($japan->inviter_sex === '男') {
$isex_m = '☑';
$isex_f = '□';
} else {
$isex_m = '□';
$isex_f = '☑';
};
$phpWord->setValue('family_name_en', $japan->family_name_en);
$phpWord->setValue('given_name_en', $japan->given_name_en);
$phpWord->setValue('family_name_cn', $japan->family_name_cn);
$phpWord->setValue('given_name_cn', $japan->given_name_cn);
$phpWord->setValue('used_name_en', $japan->used_name_en);
$phpWord->setValue('used_name_cn', $japan->used_name_cn);
$phpWord->setValue('date_birth', $japan->date_birth);
$phpWord->setValue('birth_province', $japan->birth_province);
$phpWord->setValue('birth_city', $japan->birth_city);
$phpWord->setValue('sex_male', $sex_male);
$phpWord->setValue('sex_female', $sex_female);
$phpWord->setValue('marital_ds', $marital_ds);
$phpWord->setValue('marital_yh', $marital_yh);
$phpWord->setValue('marital_sa', $marital_sa);
$phpWord->setValue('marital_lh', $marital_lh);
$phpWord->setValue('nationality', $japan->nationality);
$phpWord->setValue('other_nationality', $japan->other_nationality);
$phpWord->setValue('id_card', $japan->id_card);
$phpWord->setValue('hz_wj', $hz_wj);
$phpWord->setValue('hz_gw', $hz_gw);
$phpWord->setValue('hz_pt', $hz_pt);
$phpWord->setValue('hz_qt', $hz_qt);
$phpWord->setValue('passport_no', $japan->passport_no);
$phpWord->setValue('issue_place', $japan->issue_place);
$phpWord->setValue('issue_date', $japan->issue_date);
$phpWord->setValue('issue_authority', $japan->issue_authority);
$phpWord->setValue('expiry_date', $japan->expiry_date);
$phpWord->setValue('purpose', $japan->purpose);
$phpWord->setValue('intended_stay_date', $japan->intended_stay_date);
$phpWord->setValue('stayday', $japan->stayday);
$phpWord->setValue('entry_port', $japan->entry_port);
$phpWord->setValue('ship_airline_name', $japan->ship_airline_name);
$phpWord->setValue('hotel_friend_name', $japan->hotel_friend_name);
$phpWord->setValue('phone_japan', $japan->phone_japan);
$phpWord->setValue('address_japan', $japan->address_japan);
$phpWord->setValue('entry_date_stay_last_time', $japan->entry_date_stay_last_time);
$phpWord->setValue('address', $japan->address);
$phpWord->setValue('tel', $japan->tel);
$phpWord->setValue('phone', $japan->phone);
$phpWord->setValue('email', $japan->email);
$phpWord->setValue('company', $japan->company);
$phpWord->setValue('company_tel', $japan->company_tel);
$phpWord->setValue('company_address', $japan->company_address);
$phpWord->setValue('position', $japan->position);
$phpWord->setValue('guarantor_en', $japan->guarantor_en);
$phpWord->setValue('guarantor', $japan->guarantor);
$phpWord->setValue('guarantor_tel', $japan->guarantor_tel);
$phpWord->setValue('guarantor_address_en', $japan->guarantor_address_en);
$phpWord->setValue('guarantor_address', $japan->guarantor_address);
$phpWord->setValue('guarantor_birth', $japan->guarantor_birth);
$phpWord->setValue('guarantor_relation', $japan->guarantor_relation);
$phpWord->setValue('guarantor_position', $japan->guarantor_position);
$phpWord->setValue('guarantor_visa', $japan->guarantor_visa);
$phpWord->setValue('gsex_m', $gsex_m);
$phpWord->setValue('gsex_f', $gsex_f);
$phpWord->setValue('inviter', $japan->inviter);
$phpWord->setValue('inviter_tel', $japan->inviter_tel);
$phpWord->setValue('inviter_address', $japan->inviter_address);
$phpWord->setValue('inviter_birth', $japan->inviter_birth);
$phpWord->setValue('inviter_relation', $japan->inviter_relation);
$phpWord->setValue('inviter_position', $japan->inviter_position);
$phpWord->setValue('inviter_visa', $japan->inviter_visa);
$phpWord->setValue('note', $japan->note);
$phpWord->setValue('application_date', $japan->application_date);
$phpWord->setValue('isex_m', $isex_m);
$phpWord->setValue('isex_f', $isex_f);
$wordPath = root_path('storage/japan/words') . $id . '.docx';
$phpWord->saveAs($wordPath);
$pdfPath = root_path('storage/japan/pdfs') . $id . '.pdf';
word_to_pdf($wordPath, $pdfPath);
//响应
return response(file_get_contents($pdfPath), 200, ['Content-Type' => 'application/pdf']);