import React from "react"; import OpenSans from "../fonts/OpenSans-Medium.ttf"; import {…
- User
import React from "react"; import OpenSans from "../fonts/OpenSans-Medium.ttf"; import { Font } from "@react-pdf/renderer"; import { Document, Page, Text, View, StyleSheet } from "@react-pdf/renderer";
Font.register({ family: "OpenSans", src: OpenSans, });
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]; } });
// Tạo kiểu dữ liệu cho dữ liệu nhập từ form 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: "OpenSans", fontSize: 12, }, header: { fontSize: 18, fontWeight: "bold", marginBottom: 20, textAlign: "center", }, table: { marginTop: "60", marginBottom: "60", flexDirection: "column", borderWidth: 1, borderColor: "#ddd", borderRadius: 5, }, tableRow: { flexDirection: "row", borderBottom: "1px solid #ddd", }, tableHeader: { backgroundColor: "#f7f7f7", textAlign: "center", fontWeight: "bold", borderBottom: "1px solid #ddd", }, tableColSTT: { width: "100", borderRight: "1px solid #ddd", }, tableColTenHang: { width: "300", borderRight: "1px solid #ddd", }, tableColMaSo: { width: "100", borderRight: "1px solid #ddd", }, tableColDonViTinh: { width: "150", borderRight: "1px solid #ddd", }, tableColSoLuong: { flex: 2, width: "300", flexDirection: "row", borderRight: "1px solid #ddd", }, tableColChungTu: { flex: 1, paddingVertical: 10, paddingHorizontal: 15, borderRight: "1px solid #ddd", }, tableColThucNhap: { flex: 1, paddingVertical: 10, paddingHorizontal: 15, }, tableColTrangThai: { width: "150", },
label: { fontWeight: "bold", marginRight: 10, }, });
const PDFDocument: React.FC<{ formData: PDFFormData }> = ({ formData }) => ( PHIẾU NHẬP KHO
<View> <Text> <Text style={styles.label}>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> <Text> <Text style={styles.label}>Nhập tại kho:</Text> {formData.nhapTaiKho} </Text> <Text> <Text style={styles.label}>Địa điểm:</Text> {formData.diaDiem} </Text> </View> <View style={styles.table}> <View style={[styles.tableRow, styles.tableHeader]}> <Text style={[styles.tableColSTT]}>STT</Text> <Text style={[styles.tableColTenHang]}> {chunkSubstr("Tên hàng", 10).map((chunk, index) => ( <React.Fragment key={index}> {chunk} <br /> </React.Fragment> ))} </Text> <Text style={[styles.tableColMaSo]}>Mã số</Text> <Text style={[styles.tableColDonViTinh]}>Đơn vị tính</Text> <View style={styles.tableColSoLuong}> <Text>Số lượng</Text> <View style={styles.tableColChungTu}> <Text>Chứng từ</Text> </View> <View style={styles.tableColThucNhap}> <Text>Thực nhập</Text> </View> </View> <Text style={[styles.tableColTrangThai]}>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> <View> <Text> <Text style={styles.label}>Người phụ trách:</Text> {formData.nguoiPhuTrach} </Text> <Text> <Text style={styles.label}>Trưởng phòng:</Text> {formData.truongPhong} </Text> <Text> <Text style={styles.label}>Trưởng dự án:</Text> {formData.truongDuAn} </Text> <Text> <Text style={styles.label}>Người giao hàng (nếu có):</Text> {formData.nguoiGiaoHang} </Text> <Text> <Text style={styles.label}>Thủ kho:</Text> {formData.thuKho} </Text> </View> </Page> - Assistant
- User
- Assistant
- User
- Assistant
Để căn giữa tiêu đề bảng theo chiều dọc trong @react-pdf/renderer, bạn nên sử dụng thuộc tính
alignItemstrong StyleSheet của bạn và đặt giá trị của nó là"center".alignItemsdùng để căn chỉnh các item con trong một container (ở đây làView) có sử dụngflexDirection: 'row'hoặc , và sẽ căn giữa chúng theo chiều dọc nếu là hoặc theo chiều ngang nếu là . - User
Người phụ trách 1 Trưởng phòng 1 Trưởng dự án 1 1 1 Người giao hàng (nếu có) 1 còn nguyên vẹn Thủ kho viết in hoa các chữ trên
- Assistant
Dưới đây là cách viết lại các từ bạn yêu cầu theo dạng in hoa:
NGƯỜI PHỤ TRÁCH 1 TRƯỞNG PHÒNG 1 TRƯỞNG DỰ ÁN 1 1 1 NGƯỜI GIAO HÀNG (NẾU CÓ) 1 CÒN NGUYÊN VẸN THỦ KHOKhi bạn muốn viết hoa các chữ này trong React hoặc @react-pdf/renderer, bạn có thể sử dụng phương thức
.toUpperCase()của JavaScript để biến đổi text vào thời điểm render. Ví dụ:<Text>{formData.nguoiPhuTrach.toUpperCase()}</Text> <Text>{formData.truongPhong.toUpperCase()}</Text> <Text>{formData.truongDuAn.toUpperCase()}</Text> <Text>{"NGƯỜI GIAO HÀNG (NẾU CÓ)".toUpperCase()}</Text> <Text>{"CÒN NGUYÊN VẸN".toUpperCase()}</Text> <Text>{formData.thuKho.toUpperCase()}</Text>Nếu bạn muốn viết hoa tất cả các chữ được render bởi một component
Textnào đó, bạn cũng có thể thêm cách cấu hình CSS (trong trường hợp của web) hoặc sử dụngStyleSheet(trong @react-pdf/renderer) để thiết lập : - User
- Assistant
Để tối ưu viền trong bảng của bạn sao cho không có viền nào bị trùng lặp và gây ra hiện tượng đậm không đồng đều, bạn cần áp dụng 1 số cải tiến cho phần
StyleSheetvà cấu trúc của component bảng. Cơ bản, chúng ta sẽ chỉ thêm viền dưới cho hàng cuối cùng hoặc bỏ đi viền dưới của các hàng (trừ hàng cuối cùng) và chỉ để viền phải ở các cột cuối cùng của mỗi hàng. Dưới đây là cách bạn có thể thực hiện:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.