import React from "react"; import Medium from "../fonts/Medium.ttf"; import Bold from…
- User
import React from "react"; import Medium from "../fonts/Medium.ttf"; import Bold from "../fonts/Bold.ttf"; import { Font } from "@react-pdf/renderer"; import { Document, Page, Text, View, StyleSheet } from "@react-pdf/renderer";
Font.register({ family: "Medium", src: Medium, });
Font.register({ family: "Bold", src: Bold, });
interface Totals { totalChungTu: number; totalThucNhap: number; }
export const chunkSubstr = (str: string, size: number) => { if (str.length <= size) { return [str]; }
const numChunks = Math.ceil(str.length / size); const chunks = new Array(numChunks);
for (let i = 0, o = 0; i < numChunks; ++i, o += size) { chunks[i] = str.substring(o, size); }
return chunks; };
Font.registerHyphenationCallback((word) => { if (word.length > 12) { return chunkSubstr(word, 10); } else { return [word]; } });
export interface PDFFormData { soPhieu: string; nguoiGiao: string; donVi: string; nhapTaiKho: string; diaDiem: string; rows: { tenHang: string; ma: string; donVi: string; chungTu: number; thucNhap: number; tinhTrang: string; }[]; nguoiPhuTrach: string; truongPhong: string; truongDuAn: string; nguoiGiaoHang: string; thuKho: string; }
const styles = StyleSheet.create({ page: { padding: 30, fontFamily: "Medium", fontSize: 12, }, header: { fontSize: 18, fontFamily: "Bold", marginBottom: 10, textAlign: "center", }, table: { marginTop: "60", marginBottom: "60", flexDirection: "column", }, tableRow: { flexDirection: "row", borderBottom: "1px solid #000", },
tableHeader: { backgroundColor: "#f7f7f7", borderTop: "1px solid #000", textAlign: "center", fontFamily: "Bold", }, tableColSTT: { width: "165px", borderLeft: "1px solid #000", borderRight: "1px solid #000", }, tableColTenHang: { width: "1360px", borderRight: "1px solid #000", }, tableColMaSo: { width: "350px", borderRight: "1px solid #000", }, tableColDonViTinh: { width: "228px", borderRight: "1px solid #000", }, tableColChungTu: { width: "280px", borderRight: "1px solid #000", }, tableColThucNhap: { width: "280px", borderRight: "1px solid #000", }, tableColTrangThai: { width: "545px", borderRight: "1px solid #000", }, label: { fontWeight: "bold", marginRight: 10, }, sign: { fontFamily: "Bold", textAlign: "center", marginRight: 10, }, });
interface Totals { totalChungTu: number; totalThucNhap: number; }
const PDFDocument: React.FC<{ formData: PDFFormData }> = ({ formData }) => (
const { totalChungTu, totalThucNhap }: Totals = calculateTotal(formData.rows);
const calculateTotal = (rows: PDFFormData['rows']): Totals => { let totalChungTu: number = 0; let totalThucNhap: number = 0;
rows.forEach((row) => { totalChungTu += row.chungTu; totalThucNhap += row.thucNhap; }); return { totalChungTu, totalThucNhap };};
<View> <Text> <Text style={{ fontSize: 12, textAlign: "center" }}>Số phiếu:</Text> {formData.soPhieu} </Text> <Text> <Text style={styles.label}>Họ và tên người giao:</Text> {formData.nguoiGiao} </Text> <Text> <Text style={styles.label}>Tên đơn vị:</Text> {formData.donVi} </Text> <View style={{ flexDirection: "row" }}> <Text style={{ flex: 7 }}> <Text style={styles.label}>Nhập tại kho:</Text> {formData.nhapTaiKho} </Text> <Text style={{ flex: 3, marginLeft: 10 }}> <Text style={styles.label}>Địa điểm:</Text> {formData.diaDiem} </Text> </View> </View> <View style={styles.table}> <View style={[styles.tableRow, styles.tableHeader]}> <Text style={[styles.tableColSTT, { paddingTop: 8 }]}>STT</Text> <Text style={[styles.tableColTenHang, { paddingTop: 8 }]}> Tên hàng </Text> <Text style={[styles.tableColMaSo, { paddingTop: 8 }]}>Mã số</Text> <Text style={[styles.tableColDonViTinh, { paddingTop: 8 }]}> Đơn vị tính </Text> <View style={{ flexDirection: "column", width: "560px", }} > <Text style={{ flex: 1, borderBottom: "1px solid #000", borderRight: "1px solid #000", }} > Số lượng </Text> <View style={{ flexDirection: "row" }}> <Text style={{ flex: 1, borderRight: "1px solid #000" }}> Chứng từ </Text> <Text style={{ flex: 1, borderRight: "1px solid #000" }}> Thực nhập </Text> </View> </View> <Text style={[styles.tableColTrangThai, { paddingTop: 8 }]}> Tình trạng </Text> </View> {formData.rows.map((row, index) => ( <View style={styles.tableRow} key={index}> <Text style={[styles.tableColSTT, { textAlign: "center" }]}> {index + 1} </Text> <Text style={[styles.tableColTenHang, { textAlign: "center" }]}> {row.tenHang} </Text> <Text style={[styles.tableColMaSo, { textAlign: "center" }]}> {row.ma} </Text> <Text style={[styles.tableColDonViTinh, { textAlign: "center" }]}> {row.donVi} </Text> <Text style={[styles.tableColChungTu, { textAlign: "center" }]}> {row.chungTu} </Text> <Text style={[styles.tableColThucNhap, { textAlign: "center" }]}> {row.thucNhap} </Text> <Text style={[styles.tableColTrangThai, { textAlign: "center" }]}> {row.tinhTrang} </Text> </View> ))} <View style={[styles.tableRow, { fontFamily: "Bold" }]}> <Text style={[ styles.tableColSTT, { textAlign: "center", backgroundColor: "#f7f7f7", height: 30, paddingTop: 8, }, ]} ></Text> <Text style={[ styles.tableColTenHang, { textAlign: "left", backgroundColor: "#f7f7f7", height: 30, paddingLeft: 10, paddingTop: 8, }, ]} > Cộng </Text> <Text style={[ styles.tableColMaSo, { textAlign: "center", backgroundColor: "#f7f7f7", height: 30, paddingTop: 8, }, ]} > x </Text> <Text style={[ styles.tableColDonViTinh, { textAlign: "center", backgroundColor: "#f7f7f7", height: 30, paddingTop: 8, }, ]} > x </Text> <Text style={[ styles.tableColChungTu, { textAlign: "center", backgroundColor: "#f7f7f7", height: 30, paddingTop: 8, }, ]} > 1 </Text> <Text style={[ styles.tableColThucNhap, { textAlign: "center", backgroundColor: "#f7f7f7", height: 30, paddingTop: 8, }, ]} > 1 </Text> <Text style={[ styles.tableColTrangThai, { textAlign: "center", backgroundColor: "#f7f7f7", height: 30, paddingTop: 8, }, ]} ></Text> </View> </View> <View style={{ position: "absolute", bottom: 40, left: 0, right: 0 }}> <View style={{ flexDirection: "row", justifyContent: "space-between" }}> <View style={[styles.sign, { width: "20%" }]}> <Text>NGƯỜI PHỤ TRÁCH</Text> <Text style={{ fontSize: 12, textAlign: "center", marginTop: "70" }} > {formData.nguoiPhuTrach} </Text> </View> <View style={[styles.sign, { width: "20%" }]}> <Text>TRƯỞNG PHÒNG</Text> <Text style={{ fontSize: 12, textAlign: "center", marginTop: "70" }} > {formData.truongPhong} </Text> </View> <View style={[styles.sign, { width: "20%" }]}> <Text>TRƯỞNG DỰ ÁN</Text> <Text style={{ fontSize: 12, textAlign: "center", marginTop: "70" }} > {formData.truongDuAn} </Text> </View> <View style={[styles.sign, { width: "20%" }]}> <Text>NGƯỜI GIAO HÀNG (nếu có)</Text> <Text style={{ fontSize: 12, textAlign: "center", marginTop: "70" }} > {formData.nguoiGiaoHang} </Text> </View> <View style={[styles.sign, { width: "20%" }]}> <Text>THỦ KHO</Text> <Text style={{ fontSize: 12, textAlign: "center", marginTop: "70" }} > {formData.thuKho} </Text> </View> </View> </View> </Page> - Assistant