Skip to content

Commit

Permalink
add pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim committed Apr 3, 2020
1 parent ab65a37 commit afb410a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 28 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
}
],
"require": {
"php": ">=5.6",
"ext-mysqli": "*",
"phpunit/phpunit": "^8.5",
"mockery/mockery": "^1.3"
"mockery/mockery": "^1.3",
"mpdf/mpdf": "^8.0"
},
"autoload": {
"psr-4": {
Expand Down
72 changes: 57 additions & 15 deletions src/Data/Draw.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,54 @@

namespace Tim168\DataDict\Data;

use Mpdf\Mpdf;

class Draw
{
public function dataToHtml($tables)
public function dataToHtml($tables, $lang = 'zh-CN')
{
if (!empty($tables)) {
$html = '';
foreach ($tables as $k => $v) {
$html .= '<table border="1" cellspacing="0" cellpadding="0" align="center">';
$html .= '<caption>' . $v['TABLE_NAME'] . ' ' . $v['TABLE_COMMENT'] . '</caption>';
$html .= '<tbody><tr><th>字段名</th><th>数据类型</th><th>默认值</th>
if ($lang == 'en') {
foreach ($tables as $k => $v) {
$html .= '<table border="1" cellspacing="0" cellpadding="0" align="center">';
$html .= '<caption>' . $v['TABLE_NAME'] . ' ' . $v['TABLE_COMMENT'] . '</caption>';
$html .= '<tbody><tr><th>field_name</th><th>data_type</th><th>default</th>
<th>not_null</th>
<th>auto_increment </th><th>remarks</th></tr>';
$html .= '';
foreach ($v['COLUMN'] as $f) {
$html .= '<tr><td class="c1">' . $f['COLUMN_NAME'] . '</td>';
$html .= '<td class="c2">' . $f['COLUMN_TYPE'] . '</td>';
$html .= '<td class="c3"> ' . $f['COLUMN_DEFAULT'] . '</td>';
$html .= '<td class="c4"> ' . $f['IS_NULLABLE'] . '</td>';
$html .= '<td class="c5">' . ($f['EXTRA'] == 'auto_increment' ? 'YES' : ' ') . '</td>';
$html .= '<td class="c6"> ' . $f['COLUMN_COMMENT'] . '</td>';
$html .= '</tr>';
}
$html .= '</tbody></table></p>';
}
} else {
foreach ($tables as $k => $v) {
$html .= '<table border="1" cellspacing="0" cellpadding="0" align="center">';
$html .= '<caption>' . $v['TABLE_NAME'] . ' ' . $v['TABLE_COMMENT'] . '</caption>';
$html .= '<tbody><tr><th>字段名</th><th>数据类型</th><th>默认值</th>
<th>允许非空</th>
<th>自动递增</th><th>备注</th></tr>';
$html .= '';
foreach ($v['COLUMN'] as $f) {
$html .= '<tr><td class="c1">' . $f['COLUMN_NAME'] . '</td>';
$html .= '<td class="c2">' . $f['COLUMN_TYPE'] . '</td>';
$html .= '<td class="c3"> ' . $f['COLUMN_DEFAULT'] . '</td>';
$html .= '<td class="c4"> ' . $f['IS_NULLABLE'] . '</td>';
$html .= '<td class="c5">' . ($f['EXTRA'] == 'auto_increment' ? '' : ' ') . '</td>';
$html .= '<td class="c6"> ' . $f['COLUMN_COMMENT'] . '</td>';
$html .= '</tr>';
$html .= '';
foreach ($v['COLUMN'] as $f) {
$html .= '<tr><td class="c1">' . $f['COLUMN_NAME'] . '</td>';
$html .= '<td class="c2">' . $f['COLUMN_TYPE'] . '</td>';
$html .= '<td class="c3"> ' . $f['COLUMN_DEFAULT'] . '</td>';
$html .= '<td class="c4"> ' . $f['IS_NULLABLE'] . '</td>';
$html .= '<td class="c5">' . ($f['EXTRA'] == 'auto_increment' ? '' : ' ') . '</td>';
$html .= '<td class="c6"> ' . $f['COLUMN_COMMENT'] . '</td>';
$html .= '</tr>';
}
$html .= '</tbody></table></p>';
}
$html .= '</tbody></table></p>';
}

return $html;
} else {
return false;
Expand Down Expand Up @@ -59,4 +83,22 @@ public function ToHtml($title, $html)

return $content;
}

public function ToHtmlFile($content, $fileName)
{
$filename = $fileName . ".html";
$handle = fopen($filename, "w");
fwrite($handle, $content);
fclose($handle);
}

public function ToPdfFile($content, $path)
{
$mpdf = new Mpdf();
$mpdf->autoScriptToLang = true;
$mpdf->autoLangToFont = true;
$mpdf->WriteHTML($content);
$mpdf->Output($path . '.pdf', 'f');
}

}
4 changes: 0 additions & 4 deletions src/Data/Fetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

class Fetch
{
public function __construct()
{
}

public function conn($dbHost, $dbUserName, $dbPassWord, $dbName, $dbPort)
{
try {
Expand Down
18 changes: 10 additions & 8 deletions src/DataDict.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ class DataDict
public $dbPassWord;
public $dbName;
public $dbPort;
public $title = '数据字典';

public function __construct($dbHost, $dbUserName, $dbPassWord, $dbName, $dbPort)
public function __construct($dbHost, $dbUserName, $dbPassWord, $dbName, $dbPort = '3306')
{
$this->dbHost = $dbHost;
$this->dbUserName = $dbUserName;
Expand All @@ -23,17 +22,20 @@ public function __construct($dbHost, $dbUserName, $dbPassWord, $dbName, $dbPort)
$this->dbPort = $dbPort;
}

public function get()
public function get($fileName = 'dict', $type = 'html', $lang = 'zh-CN')
{
$fetch = new Fetch();
$tables = $fetch->conn($this->dbHost, $this->dbUserName, $this->dbPassWord, $this->dbName, $this->dbPort);
var_dump($tables);
exit();
if (!empty($tables)) {
$draw = new Draw();
$html = $draw->dataToHtml($tables);
$content = $draw->ToHtml($this->title, $html);
return $content;
$html = $draw->dataToHtml($tables, $lang);
$content = $draw->ToHtml($lang == 'zh-CN' ? '数据字典' : 'Data Dict', $html);
if ($type == 'pdf') {
$draw->ToPdfFile($content,$fileName);
} else {
$draw->ToHtmlFile($content, $fileName);
}
}
return true;
}
}

0 comments on commit afb410a

Please sign in to comment.