fixed space to tabs
This commit is contained in:
370
src/dotted.php
370
src/dotted.php
@ -13,37 +13,37 @@ $pdf = new \Com\Tecnick\Pdf\Tcpdf();
|
||||
*/
|
||||
function readParams(): array
|
||||
{
|
||||
if (PHP_SAPI === 'cli') {
|
||||
$options = \getopt('', ['dot-size:', 'space:']);
|
||||
if (PHP_SAPI === 'cli') {
|
||||
$options = \getopt('', ['dot-size:', 'space:']);
|
||||
|
||||
return [
|
||||
'dot-size' => parseFloatParam($options['dot-size'] ?? null, 0.35),
|
||||
'space' => parseFloatParam($options['space'] ?? null, 5.0),
|
||||
];
|
||||
}
|
||||
return [
|
||||
'dot-size' => parseFloatParam($options['dot-size'] ?? null, 0.35),
|
||||
'space' => parseFloatParam($options['space'] ?? null, 5.0),
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'dot-size' => parseFloatParam($_GET['dot-size'] ?? null, 0.35),
|
||||
'space' => parseFloatParam($_GET['space'] ?? null, 5.0),
|
||||
];
|
||||
return [
|
||||
'dot-size' => parseFloatParam($_GET['dot-size'] ?? null, 0.35),
|
||||
'space' => parseFloatParam($_GET['space'] ?? null, 5.0),
|
||||
];
|
||||
}
|
||||
|
||||
function parseFloatParam(mixed $value, float $default): float
|
||||
{
|
||||
if ($value === null || $value === '') {
|
||||
return $default;
|
||||
}
|
||||
if ($value === null || $value === '') {
|
||||
return $default;
|
||||
}
|
||||
|
||||
if (\is_array($value)) {
|
||||
fail('Parameter value must be a single number.');
|
||||
}
|
||||
if (\is_array($value)) {
|
||||
fail('Parameter value must be a single number.');
|
||||
}
|
||||
|
||||
$normalized = \str_replace(',', '.', (string) $value);
|
||||
if (! \is_numeric($normalized)) {
|
||||
fail('Parameter value must be numeric.');
|
||||
}
|
||||
$normalized = \str_replace(',', '.', (string) $value);
|
||||
if (! \is_numeric($normalized)) {
|
||||
fail('Parameter value must be numeric.');
|
||||
}
|
||||
|
||||
return (float) $normalized;
|
||||
return (float) $normalized;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -51,103 +51,103 @@ function parseFloatParam(mixed $value, float $default): float
|
||||
*/
|
||||
function validateParams(array $params): void
|
||||
{
|
||||
if ($params['dot-size'] <= 0 || $params['dot-size'] > 2) {
|
||||
fail('Invalid --dot-size value. Use a number greater than 0 and at most 2 mm.');
|
||||
}
|
||||
if ($params['dot-size'] <= 0 || $params['dot-size'] > 2) {
|
||||
fail('Invalid --dot-size value. Use a number greater than 0 and at most 2 mm.');
|
||||
}
|
||||
|
||||
if ($params['space'] < 2 || $params['space'] > 20) {
|
||||
fail('Invalid --space value. Use a number from 2 to 20 mm.');
|
||||
}
|
||||
if ($params['space'] < 2 || $params['space'] > 20) {
|
||||
fail('Invalid --space value. Use a number from 2 to 20 mm.');
|
||||
}
|
||||
}
|
||||
|
||||
function fail(string $message): never
|
||||
{
|
||||
if (PHP_SAPI === 'cli') {
|
||||
\fwrite(STDERR, 'Error: ' . $message . PHP_EOL);
|
||||
exit(1);
|
||||
}
|
||||
if (PHP_SAPI === 'cli') {
|
||||
\fwrite(STDERR, 'Error: ' . $message . PHP_EOL);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
\http_response_code(400);
|
||||
\header('Content-Type: text/plain; charset=utf-8');
|
||||
echo 'Error: ' . $message;
|
||||
exit;
|
||||
\http_response_code(400);
|
||||
\header('Content-Type: text/plain; charset=utf-8');
|
||||
echo 'Error: ' . $message;
|
||||
exit;
|
||||
}
|
||||
|
||||
function getLineStyle(float $width, string $color): array
|
||||
{
|
||||
return [
|
||||
'lineWidth' => $width,
|
||||
'lineCap' => 'butt',
|
||||
'lineJoin' => 'miter',
|
||||
'dashArray' => [],
|
||||
'dashPhase' => 0,
|
||||
'lineColor' => $color,
|
||||
];
|
||||
return [
|
||||
'lineWidth' => $width,
|
||||
'lineCap' => 'butt',
|
||||
'lineJoin' => 'miter',
|
||||
'dashArray' => [],
|
||||
'dashPhase' => 0,
|
||||
'lineColor' => $color,
|
||||
];
|
||||
}
|
||||
|
||||
function getFillStyle(string $color): array
|
||||
{
|
||||
return [
|
||||
'all' => [
|
||||
'lineWidth' => 0,
|
||||
'lineCap' => 'butt',
|
||||
'lineJoin' => 'miter',
|
||||
'dashArray' => [],
|
||||
'dashPhase' => 0,
|
||||
'lineColor' => $color,
|
||||
'fillColor' => $color,
|
||||
],
|
||||
];
|
||||
return [
|
||||
'all' => [
|
||||
'lineWidth' => 0,
|
||||
'lineCap' => 'butt',
|
||||
'lineJoin' => 'miter',
|
||||
'dashArray' => [],
|
||||
'dashPhase' => 0,
|
||||
'lineColor' => $color,
|
||||
'fillColor' => $color,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{x: float, y: float, width: float, height: float} $tableBox
|
||||
*/
|
||||
function drawDotGrid(
|
||||
\Com\Tecnick\Pdf\Tcpdf $pdf,
|
||||
float $left,
|
||||
float $top,
|
||||
float $right,
|
||||
float $bottom,
|
||||
float $dotSize,
|
||||
float $space,
|
||||
array $tableBox
|
||||
\Com\Tecnick\Pdf\Tcpdf $pdf,
|
||||
float $left,
|
||||
float $top,
|
||||
float $right,
|
||||
float $bottom,
|
||||
float $dotSize,
|
||||
float $space,
|
||||
array $tableBox
|
||||
): void {
|
||||
$radius = $dotSize / 2.0;
|
||||
$dotStyle = getFillStyle('rgb(145,145,145)');
|
||||
$radius = $dotSize / 2.0;
|
||||
$dotStyle = getFillStyle('rgb(145,145,145)');
|
||||
|
||||
for ($y = $top; $y <= $bottom; $y += $space) {
|
||||
for ($x = $left; $x <= $right; $x += $space) {
|
||||
$insideTable = (
|
||||
$x >= $tableBox['x']
|
||||
&& $x <= ($tableBox['x'] + $tableBox['width'])
|
||||
&& $y >= $tableBox['y']
|
||||
&& $y <= ($tableBox['y'] + $tableBox['height'])
|
||||
);
|
||||
for ($y = $top; $y <= $bottom; $y += $space) {
|
||||
for ($x = $left; $x <= $right; $x += $space) {
|
||||
$insideTable = (
|
||||
$x >= $tableBox['x']
|
||||
&& $x <= ($tableBox['x'] + $tableBox['width'])
|
||||
&& $y >= $tableBox['y']
|
||||
&& $y <= ($tableBox['y'] + $tableBox['height'])
|
||||
);
|
||||
|
||||
if ($insideTable) {
|
||||
continue;
|
||||
}
|
||||
if ($insideTable) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$pdf->page->addContent($pdf->graph->getCircle($x, $y, $radius, 0, 360, 'F', $dotStyle));
|
||||
}
|
||||
}
|
||||
$pdf->page->addContent($pdf->graph->getCircle($x, $y, $radius, 0, 360, 'F', $dotStyle));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function drawFrame(\Com\Tecnick\Pdf\Tcpdf $pdf, float $pageWidth, float $pageHeight): void
|
||||
{
|
||||
$outerMargin = 5.0;
|
||||
$outerMargin = 5.0;
|
||||
|
||||
$pdf->page->addContent(
|
||||
$pdf->graph->getRect(
|
||||
$outerMargin,
|
||||
$outerMargin,
|
||||
$pageWidth - (2 * $outerMargin),
|
||||
$pageHeight - (2 * $outerMargin),
|
||||
'D',
|
||||
['all' => getLineStyle(0.18, 'black')]
|
||||
)
|
||||
);
|
||||
$pdf->page->addContent(
|
||||
$pdf->graph->getRect(
|
||||
$outerMargin,
|
||||
$outerMargin,
|
||||
$pageWidth - (2 * $outerMargin),
|
||||
$pageHeight - (2 * $outerMargin),
|
||||
'D',
|
||||
['all' => getLineStyle(0.18, 'black')]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -155,66 +155,66 @@ function drawFrame(\Com\Tecnick\Pdf\Tcpdf $pdf, float $pageWidth, float $pageHei
|
||||
*/
|
||||
function drawTitleBlock(\Com\Tecnick\Pdf\Tcpdf $pdf, array $tableBox): void
|
||||
{
|
||||
assertFontIsAvailable();
|
||||
assertFontIsAvailable();
|
||||
|
||||
$font = $pdf->font->insert($pdf->pon, 'helvetica', '', 7.0, 0.0, 1.0);
|
||||
$pdf->page->addContent($font['out']);
|
||||
$pdf->page->addContent($pdf->color->getPdfColor('black'));
|
||||
$font = $pdf->font->insert($pdf->pon, 'helvetica', '', 7.0, 0.0, 1.0);
|
||||
$pdf->page->addContent($font['out']);
|
||||
$pdf->page->addContent($pdf->color->getPdfColor('black'));
|
||||
|
||||
$lineStyle = getLineStyle(0.16, 'black');
|
||||
$rowHeight = $tableBox['height'] / 4.0;
|
||||
$labelWidth = 28.0;
|
||||
$lineStyle = getLineStyle(0.16, 'black');
|
||||
$rowHeight = $tableBox['height'] / 4.0;
|
||||
$labelWidth = 28.0;
|
||||
|
||||
$pdf->page->addContent(
|
||||
$pdf->graph->getRect($tableBox['x'], $tableBox['y'], $tableBox['width'], $tableBox['height'], 'D', [
|
||||
'all' => $lineStyle,
|
||||
])
|
||||
);
|
||||
$pdf->page->addContent(
|
||||
$pdf->graph->getRect($tableBox['x'], $tableBox['y'], $tableBox['width'], $tableBox['height'], 'D', [
|
||||
'all' => $lineStyle,
|
||||
])
|
||||
);
|
||||
|
||||
for ($row = 1; $row < 4; ++$row) {
|
||||
$lineY = $tableBox['y'] + ($row * $rowHeight);
|
||||
$pdf->page->addContent(
|
||||
$pdf->graph->getLine($tableBox['x'], $lineY, $tableBox['x'] + $tableBox['width'], $lineY, $lineStyle)
|
||||
);
|
||||
}
|
||||
for ($row = 1; $row < 4; ++$row) {
|
||||
$lineY = $tableBox['y'] + ($row * $rowHeight);
|
||||
$pdf->page->addContent(
|
||||
$pdf->graph->getLine($tableBox['x'], $lineY, $tableBox['x'] + $tableBox['width'], $lineY, $lineStyle)
|
||||
);
|
||||
}
|
||||
|
||||
$pdf->page->addContent(
|
||||
$pdf->graph->getLine(
|
||||
$tableBox['x'] + $labelWidth,
|
||||
$tableBox['y'],
|
||||
$tableBox['x'] + $labelWidth,
|
||||
$tableBox['y'] + $tableBox['height'],
|
||||
$lineStyle
|
||||
)
|
||||
);
|
||||
$pdf->page->addContent(
|
||||
$pdf->graph->getLine(
|
||||
$tableBox['x'] + $labelWidth,
|
||||
$tableBox['y'],
|
||||
$tableBox['x'] + $labelWidth,
|
||||
$tableBox['y'] + $tableBox['height'],
|
||||
$lineStyle
|
||||
)
|
||||
);
|
||||
|
||||
$labels = ['Názov projektu', 'Autor', 'Dátum', 'Revízia'];
|
||||
foreach ($labels as $index => $label) {
|
||||
$pdf->page->addContent(
|
||||
$pdf->getTextCell(
|
||||
$label,
|
||||
$tableBox['x'] + 2.0,
|
||||
$tableBox['y'] + ($index * $rowHeight) + 2.7,
|
||||
$labelWidth - 4.0,
|
||||
4.0,
|
||||
valign: 'C',
|
||||
halign: 'L',
|
||||
drawcell: false
|
||||
)
|
||||
);
|
||||
}
|
||||
$labels = ['Názov projektu', 'Autor', 'Dátum', 'Revízia'];
|
||||
foreach ($labels as $index => $label) {
|
||||
$pdf->page->addContent(
|
||||
$pdf->getTextCell(
|
||||
$label,
|
||||
$tableBox['x'] + 2.0,
|
||||
$tableBox['y'] + ($index * $rowHeight) + 2.7,
|
||||
$labelWidth - 4.0,
|
||||
4.0,
|
||||
valign: 'C',
|
||||
halign: 'L',
|
||||
drawcell: false
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function assertFontIsAvailable(): void
|
||||
{
|
||||
$fontFile = K_PATH_FONTS . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'helvetica.json';
|
||||
if (! \is_readable($fontFile)) {
|
||||
fail(
|
||||
'Missing font file helvetica.json. Generate fonts first: '
|
||||
. 'cd vendor\\tecnickcom\\tc-lib-pdf-font\\util && composer install && cd .. && composer install '
|
||||
. '&& cd util && php bulk_convert.php'
|
||||
);
|
||||
}
|
||||
$fontFile = K_PATH_FONTS . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'helvetica.json';
|
||||
if (! \is_readable($fontFile)) {
|
||||
fail(
|
||||
'Missing font file helvetica.json. Generate fonts first: '
|
||||
. 'cd vendor\\tecnickcom\\tc-lib-pdf-font\\util && composer install && cd .. && composer install '
|
||||
. '&& cd util && php bulk_convert.php'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -222,63 +222,63 @@ function assertFontIsAvailable(): void
|
||||
*/
|
||||
function buildPdf(\Com\Tecnick\Pdf\Tcpdf $pdf, array $params): string
|
||||
{
|
||||
$pageWidth = 210.0;
|
||||
$pageHeight = 297.0;
|
||||
$innerMargin = 10.0;
|
||||
$tableBox = [
|
||||
'width' => 80.0,
|
||||
'height' => 40.0,
|
||||
'x' => $pageWidth - $innerMargin - 80.0,
|
||||
'y' => $pageHeight - $innerMargin - 40.0,
|
||||
];
|
||||
$pageWidth = 210.0;
|
||||
$pageHeight = 297.0;
|
||||
$innerMargin = 10.0;
|
||||
$tableBox = [
|
||||
'width' => 80.0,
|
||||
'height' => 40.0,
|
||||
'x' => $pageWidth - $innerMargin - 80.0,
|
||||
'y' => $pageHeight - $innerMargin - 40.0,
|
||||
];
|
||||
|
||||
$pdf->setCreator('pdfCreator');
|
||||
$pdf->setAuthor('');
|
||||
$pdf->setSubject('Dotted technical drawing paper');
|
||||
$pdf->setTitle('Dotted technical drawing paper');
|
||||
$pdf->setKeywords('PDF dotted grid technical drawing floor plan');
|
||||
$pdf->setPDFFilename('dotted.pdf');
|
||||
$pdf->setCreator('pdfCreator');
|
||||
$pdf->setAuthor('');
|
||||
$pdf->setSubject('Dotted technical drawing paper');
|
||||
$pdf->setTitle('Dotted technical drawing paper');
|
||||
$pdf->setKeywords('PDF dotted grid technical drawing floor plan');
|
||||
$pdf->setPDFFilename('dotted.pdf');
|
||||
|
||||
$pdf->addPage(['format' => 'A4', 'orientation' => 'P']);
|
||||
$pdf->addPage(['format' => 'A4', 'orientation' => 'P']);
|
||||
|
||||
drawFrame($pdf, $pageWidth, $pageHeight);
|
||||
drawDotGrid(
|
||||
$pdf,
|
||||
$innerMargin,
|
||||
$innerMargin,
|
||||
$pageWidth - $innerMargin,
|
||||
$pageHeight - $innerMargin,
|
||||
$params['dot-size'],
|
||||
$params['space'],
|
||||
$tableBox
|
||||
);
|
||||
drawTitleBlock($pdf, $tableBox);
|
||||
drawFrame($pdf, $pageWidth, $pageHeight);
|
||||
drawDotGrid(
|
||||
$pdf,
|
||||
$innerMargin,
|
||||
$innerMargin,
|
||||
$pageWidth - $innerMargin,
|
||||
$pageHeight - $innerMargin,
|
||||
$params['dot-size'],
|
||||
$params['space'],
|
||||
$tableBox
|
||||
);
|
||||
drawTitleBlock($pdf, $tableBox);
|
||||
|
||||
return $pdf->getOutPDFString();
|
||||
return $pdf->getOutPDFString();
|
||||
}
|
||||
|
||||
function outputPdf(\Com\Tecnick\Pdf\Tcpdf $pdf, string $rawPdf): void
|
||||
{
|
||||
if (PHP_SAPI !== 'cli') {
|
||||
if (\ob_get_length() !== false && \ob_get_length() > 0) {
|
||||
\ob_clean();
|
||||
}
|
||||
if (PHP_SAPI !== 'cli') {
|
||||
if (\ob_get_length() !== false && \ob_get_length() > 0) {
|
||||
\ob_clean();
|
||||
}
|
||||
|
||||
$pdf->renderPDF($rawPdf);
|
||||
return;
|
||||
}
|
||||
$pdf->renderPDF($rawPdf);
|
||||
return;
|
||||
}
|
||||
|
||||
$outputDir = \dirname(__DIR__) . DIRECTORY_SEPARATOR . 'output';
|
||||
if (! \is_dir($outputDir) && ! \mkdir($outputDir, 0775, true) && ! \is_dir($outputDir)) {
|
||||
fail('Unable to create output directory: ' . $outputDir);
|
||||
}
|
||||
$outputDir = \dirname(__DIR__) . DIRECTORY_SEPARATOR . 'output';
|
||||
if (! \is_dir($outputDir) && ! \mkdir($outputDir, 0775, true) && ! \is_dir($outputDir)) {
|
||||
fail('Unable to create output directory: ' . $outputDir);
|
||||
}
|
||||
|
||||
$outputFile = $outputDir . DIRECTORY_SEPARATOR . 'dotted.pdf';
|
||||
if (\file_put_contents($outputFile, $rawPdf) === false) {
|
||||
fail('Unable to write PDF file: ' . $outputFile);
|
||||
}
|
||||
$outputFile = $outputDir . DIRECTORY_SEPARATOR . 'dotted.pdf';
|
||||
if (\file_put_contents($outputFile, $rawPdf) === false) {
|
||||
fail('Unable to write PDF file: ' . $outputFile);
|
||||
}
|
||||
|
||||
echo 'PDF generated: ' . $outputFile . PHP_EOL;
|
||||
echo 'PDF generated: ' . $outputFile . PHP_EOL;
|
||||
}
|
||||
|
||||
$params = readParams();
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Igor\Pdfcreator;
|
||||
require __DIR__.'/../vendor/autoload.php';
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
\define('K_PATH_FONTS', \realpath(__DIR__ . '/../vendor/tecnickcom/tc-lib-pdf-font/target/fonts'));
|
||||
|
||||
@ -16,12 +17,12 @@ $pdf->page->addContent($bfont['out']);
|
||||
$html = '<h1>Hello, PDF!</h1><p>Generated with tc-lib-pdf.</p>';
|
||||
|
||||
$pdf->addHTMLCell(
|
||||
html: $html,
|
||||
posx: 15, // mm from left page edge
|
||||
posy: 20, // mm from top page edge
|
||||
width: 180, // mm wide (0 = to right margin)
|
||||
html: $html,
|
||||
posx: 15, // mm from left page edge
|
||||
posy: 20, // mm from top page edge
|
||||
width: 180, // mm wide (0 = to right margin)
|
||||
);
|
||||
|
||||
$rawpdf = $pdf->getOutPDFString();
|
||||
|
||||
$pdf->renderPDF($rawpdf);
|
||||
$pdf->renderPDF($rawpdf);
|
||||
|
||||
Reference in New Issue
Block a user