ratiw / jsonpdf
JsonPDF 是 FPDF 的包装类,用于使用 JSON 数据创建 PDF。
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-28 15:28:54 UTC
README
JsonPDF 是一个用于从 JSON 数据创建 PDF 文档的 FPDF 包装类。
使用 Composer 安装
只需将需求添加到您的 composer.json
文件中。
{
"require": {
"ratiw/jsonpdf": "~1.0"
}
}
示例
<?php
require "vendor/autoload.php";
// This part usually comes from file or http request
$document = array(
'body' => array(
array(
'type' => 'text',
'width' => 40,
'height' => 10,
'text' => 'Hello World!',
'font' => 'Arial',
'font-style' => 'B',
'font-size' => 16,
),
array(
'type' => 'text',
'text' => 'Hi, {name}!',
),
),
);
$data = array(
'name' => 'Rati'
);
// The main code is here
$pdf = new ratiw\JsonPDF\JsonPDF('P', 'mm', 'A4');
$pdf->make(json_encode($document), json_encode($data), 'both');
// $pdf->make(json_encode($document), json_encode($data), 'form');
// $pdf->make(json_encode($document), json_encode($data), 'data');
$pdf->render();
?>
在 examples
目录中查看更多示例。
JSON 数据结构
{
"header": [..array of object..],
"footer": [..array of object..],
"body": [..array of object..],
"settings": [
"title": "Document Title",
"author": "Author name",
"subject": "Subject",
"creator": "Generator name",
"keywords": "list of keywords here",
"defaultFont": [
"name": "FontName",
"size": 16
]
],
"fonts": [..array of font to be added..],
"data": [..array of data..],
"tables": [..table definition..]
}
对象
JsonPDF 中有 5 种对象类型。每个对象都执行特定的任务以渲染 PDF 文档。
每个对象都共享一些共同属性,如绘制颜色、填充颜色(背景颜色)、x-y 位置等。
以下对象类型可用:
- 文本
- 线条
- 矩形
- 图像
- 表格
常见对象属性
以下属性是所有对象的共同属性,这意味着它们可以设置在任何对象上。它们可能会或可能不会影响对象的渲染,具体取决于该对象是否使用该属性。
属性
-
x
(可选) 对象的起始水平位置。 -
y
(可选) 对象的起始垂直位置。 -
font
(可选) 字体名称。 -
font-style
(可选) 字体样式:N, B, BI -
font-size
(可选) 字体大小 -
text-color
(可选) 文本颜色。如果省略,将使用当前文本颜色。 -
draw-color
(可选) 线颜色。如果省略,将使用当前线颜色。 -
fill-color
(可选) 填充颜色,如果省略,将使用当前填充颜色。 -
line-width
(可选) 线宽。如果省略,将使用默认值(0.2mm,如 FPDF 文档中指定)。 -
render-as
(可选) 指定此对象是否应作为form
或作为data
进行渲染。
render-as
属性
JsonPDF 可以以三种不同的模式进行渲染。仅渲染标记为 form
的对象(《RENDER_FORM_ONLY》),仅渲染标记为 data
的对象(《RENDER_DATA_ONLY》),或者渲染 form
和 data
(《RENDER_ALL》)。
渲染的文档可以显示为空白表单(《RENDER_FORM_ONLY》),也可以显示为原始数据(《RENDER_DATA_ONLY》),以打印在预先制作好的纸张表单上,或者可以显示为两者(《RENDER_ALL》),以打印在普通纸张上。
默认情况下,如果未定义对象的 render-as
属性,则对象将视为 RENDER_ALL
。这意味着它将作为 form
和 data
的一部分进行渲染。
文本对象
使用 text
对象在指定位置显示文本。可以通过在 text
对象的 text 属性中嵌入花括号 {}
来将数据绑定到 text
对象。
以下示例显示了嵌入到文本属性中的 username
数据。此 username
将由 JSON 的 data
部分中提供的实际数据替换。
"body": [
{
"type": "text",
"x": 10,
"y": 20,
"text": "Hello, {username}. How are you today!",
"text-color": "255,125,125"
}
],
"data": [
"username": "Rati"
]
其他属性
-
align
(可选) -
ln
(可选) -
border
(可选) -
text
(可选) -
width
(可选) -
height
(可选) -
multiline
(可选)
线条对象
《line》对象用于在指定位置绘制直线,位置由x1
、y1
、x2
、y2
确定。
属性
-
x1
、y1
为直线的起始位置。 -
x2
、y2
为直线的结束位置。如果省略x2
,则直线将绘制到文档的右边缘。如果省略y2
,则它将假设与y1
相同的值,从而绘制一条直线。
矩形对象
《rect》对象使用指定的x
、y
、width
、height
属性在文档上绘制矩形。
属性
-
x
、y
为对象的起始位置。 -
宽度
-
高度
-
radius
(可选)如果提供,将绘制圆角矩形。 -
style
(可选)
图像对象
《image》对象用于在文档的指定位置、width
和height
上绘制给定的图像。
属性
-
url
图像的URL。 -
width
(可选) -
height
(可选)
表格、表头、表体对象
《table》对象用于在文档上绘制数据表。表属性必须在tables
部分定义,数据必须在JSON的data
部分存在。
《table》对象将渲染包含表头和表体的完整表格。
《table-header》对象将仅渲染给定表格的标题部分。
《table-body》对象将仅渲染给定表格的表体部分。
属性
- 《table》在
tables
部分定义的表的名称。
设置
本节用于设置PDF文档的各种属性。
"settings": {
"title": "Test PDF Document",
"author": "Rati Wannapanop",
"creator": "JsonPDF"
}
属性
alias-nb-pages
left-margin
top-margin
right-margin
auto-pagebreak
auto-agebreak-margin
compression
zoom
layout
default-font
utf8
author
title
subject
keywords
creator
header-height
定义字体
您可以在本节中通过提供fontname
、fontstyle
和fontfile
的数组来添加自定义字体,以用于您的PDF文档。
为了使用您自己的自定义字体,您必须使用FPDF的MakeFont函数创建fontfile
。
有关更多信息,请参阅FPDF网站。
"fonts": [
["THSarabun", "", "THSarabun.php"],
["THSarabun", "B", "THSarabun Bold.php"],
["THSarabun", "I", "THSarabun Italic.php"],
["THSarabun", "BI", "THSarabun Bold Italic.php"]
]
数据绑定
您在本节的data
部分定义变量的数据。变量名用花括号括起来,例如{name}
。
变量通常嵌入到《Text》对象的text
属性中。
"body": [
{
'type': 'text',
'text': 'Hello, {name}! Today is {date}.'
}
],
//
// ....
//
"data": {
"name": "Rati Wannapanop",
"date": "17/11/2556",
"table1": [
["country": "Austria", "capital": "Vienna", "area": "83,859", "pop": "8,075"],
["country": "Belgium", "capital": "Brussels", "area": "30,518", "pop": "10,192"],
["country": "Denmark", "capital": "Copenhagen", "area": "43,094", "pop": "5,295"]
]
}
表格定义
您可以在本节中定义表格结构。每个表格定义由3个属性组成:columns
、data
和style
。
rows-per-page
属性(可选)指定每页应显示的行数。如果需要,将显示空白行。
columns
属性定义给定表格的每个列特性。请参阅下文表格列。
data
属性指定在给定表格内用于数据渲染的data
部分的哪个键。
style
属性定义给定表格应该如何渲染。style
属性是可选的,如果省略,将使用默认值。请参阅下文表格样式。
"tables": [
["world_info_table": {
"rows-per-page": 10,
"columns": [
{
"name": "country",
"width": 45,
"title": "Country",
"title-align": "L",
"data-align": "L"
},
...
...
{
"name": "pop",
"width": 50,
"title": "Pop. (thousands)",
"title-align": "C",
"data-align": "R"
}
],
"data": "world_info_data",
"style": {
"border-color": "50,55,200",
"border": "LR",
"title-row": {
"height": 8,
"text-color": "200,100,50",
"fill-color": "100,50,50"
},
"data-row": {
"height": 8,
"text-color": "0,0,0",
"fill-color": "224,235,255",
"striped": true,
}
}
}],
["second_table": {
...
...
}]
],
表格列
"columns": [
{"name": "country", "width": 45, "title": "Country", "title-align": "L", "data-align": "L"},
{"name": "capital", "width": 40, "title": "Capital", "data-align": "L"},
{"name": "area", "width": 45, "title": "Area (sq km)", "title-align": "C", "data-align": "R"},
{"name": "pop", "width": 50, "title": "Pop. (thousands)", "title-align": "C", "data-align": "R"}
]
name
-- 列名width
-- 列宽title
-- 列标题title-align
-- 列标题对齐方式data-align
-- 数据列对齐方式
表格样式 表格style
属性允许用户定义表格的外观。
"style": {
"border-color": "50,55,200",
"title-row": {
"text-color": "200,100,50",
"fill-color": "100,50,50",
},
"data-row": {
"height": 8,
"text-color": "0,0,0",
"fill-color": "224,235,255",
"striped": true
}
}
-
border-color
-- 指定表格的边框颜色。 -
title-row
-- 定义标题行应该如何渲染。height
-- 行高text-color
fill-color
font
font-style
font-size
-
data-row
-- 定义数据行应该如何渲染。height
-- 行高text-color
fill-color
条纹
font
font-style
font-size
实用函数
snakeToCamel
deepMerge
或deep_merge