baks-dev/materials-sign
Laravel/PHP модуль для работы с «Честным знаком» по сырью: импорт и обработка кодов маркировки, генерация/печать этикеток и штрихкодов. Поддерживает загрузку файлов, обрезку PDF (pdfcrop/ImageMagick) и установку ассетов/миграций.
Install Dependencies
composer require baks-dev/materials-sign baks-dev/barcode phpoffice/phpspreadsheet
Ensure PHP 8.4+ is installed.
Set Up File Storage Create and configure the upload directory:
mkdir -p public/upload/material_sign_code
chown -R www-data:www-data public/upload/material_sign_code # Adjust user/group as needed
Install System Tools On Ubuntu/Debian:
sudo apt install pdftk imagemagick texlive-extra-utils
Configure sudo for pdfcrop (add to /etc/sudoers):
www-data ALL=(ALL) NOPASSWD: /usr/bin/pdfcrop
Update ImageMagick policy (/etc/ImageMagick-6/policy.xml):
<policy domain="coder" rights="read|write" pattern="PDF"/>
Run Asset Installation
php artisan baks:assets:install # Assumes SymfonyBridge or custom Artisan wrapper
Database Setup Generate and run migrations:
php artisan doctrine:migrations:diff
php artisan doctrine:migrations:migrate
Note: Laravel users must adapt Doctrine commands to Eloquent or use a bridge like symfony/bridge.
First Use Case
Upload a material sign PDF via the package’s controller (e.g., MaterialSignController@upload). Verify:
baks-dev/barcode).public/upload/material_sign_code/ for processed files).MaterialSignController).baks-dev/barcode) and embeds them in the PDF.pdfcrop and saves to public/upload/material_sign_code/.// Pseudocode for Laravel integration
use BaksDev\MaterialsSign\Services\MaterialSignProcessor;
$processor = new MaterialSignProcessor();
$processedPath = $processor->processUpload(
$request->file('material_sign_pdf'),
$materialId
);
baks:material-sign:batch command (if available) or loop through files:
foreach ($materialFiles as $file) {
$processor->processUpload($file, $materialId);
}
php artisan vendor:publish --tag=materials-sign-assets
resources/views/materials-sign/ templates to match branding.baks-dev/barcode for dynamic codes:
use BaksDev\Barcode\BarcodeGenerator;
$barcode = BarcodeGenerator::generate(
'DATA:' . $material->code,
'QRCODE',
['margin' => 10]
);
$processor->embedBarcode($barcode, $pdfPath);
material_signs table (migration example):
// Schema for Laravel (adapt from Doctrine)
Schema::create('material_signs', function (Blueprint $table) {
$table->id();
$table->string('material_code');
$table->string('file_path');
$table->string('barcode_data');
$table->timestamps();
});
Symfony/Laravel Bridge:
Use symfony/bridge or wrap Symfony components in Laravel services. Example:
// app/Services/MaterialSignService.php
class MaterialSignService {
protected $processor;
public function __construct() {
$this->processor = new \BaksDev\MaterialsSign\Services\MaterialSignProcessor();
}
public function process($file) {
return $this->processor->processUpload($file, auth()->id());
}
}
Event-Driven Workflows: Trigger actions after processing:
event(new MaterialSignProcessed($materialSign));
Listen for updates (e.g., send notifications):
MaterialSignProcessed::listen(function ($event) {
Notification::send($event->user, new MaterialSignReady($event->filePath));
});
Testing: Mock PDF processing in unit tests:
$processor = $this->mock(MaterialSignProcessor::class);
$processor->shouldReceive('cropPdf')->andReturn('cropped.pdf');
API Endpoints: Expose processing via Laravel API:
Route::post('/material-signs', [MaterialSignController::class, 'store']);
// MaterialSignController.php
public function store(Request $request) {
$path = app(MaterialSignService::class)->process($request->file('pdf'));
return response()->json(['path' => $path]);
}
PDF Processing Dependencies:
pdftk/imagemagick may fail silently if permissions or policies are misconfigured.sudo -u www-data pdfcrop input.pdf
Check logs in /var/log/syslog for errors.Doctrine vs. Eloquent Conflicts:
doctrine/dbal for raw SQL or rewrite migrations with Eloquent:
// Example: Convert Doctrine migration to Eloquent
Schema::create('material_signs', function (Blueprint $table) {
$table->id();
$table->string('material_code')->unique();
$table->string('file_path');
$table->timestamps();
});
Barcode Generation Failures:
baks-dev/barcode may throw errors if fonts or libraries are missing.libgd and libpng are installed:
sudo apt install libgd-dev libpng-dev
File Path Hardcoding:
public/upload/material_sign_code).// config/materials-sign.php
'storage' => [
'path' => storage_path('app/material_signs'),
],
Update the service to use the config path.Memory Limits:
memory_limit.php.ini or process files in chunks:
ini_set('memory_limit', '512M');
Timezone Issues:
date_default_timezone_set('Europe/Moscow'); // Adjust as needed
Log PDF Processing:
Add logging to MaterialSignProcessor:
\Log::info('PDF processing started', ['file' => $file->getPathname()]);
\Log::debug('PDF crop command', ['command' => $this->getCropCommand($file)]);
Validate Barcodes: Use a QR/barcode scanner to verify generated codes match input data.
Check File Permissions:
Ensure the web server user (e.g., www-data) has write access to upload directories:
ls -la public/upload/material_sign_code
Test PDF Cropping:
Manually run pdfcrop to isolate issues:
pdfcrop --demo input.pdf output.pdf
Custom PDF Templates:
resources/views/materials-sign/templates/ to modify layouts.MaterialSignTemplate class to add dynamic content.Additional Barcode Types:
BarcodeGenerator to support new formats (e.g., DataMatrix):
public static function generateDataMatrix(string $data, array $options = []): string {
// Custom implementation
}
Blockchain Integration:
MaterialSignProcessed event to send data to a blockchain:
MaterialSignProcessed::listen
How can I help you explore Laravel packages today?