Php Şeklinde Data Çekip Pdf Dosya Oluşturcak ?
4
●178
- 21-02-2025, 01:37:16PHP ile veri çekerek PDF dosyası oluşturacak bir sistem istiyorum. Manuel olarak tek tek PDF düzenlemek yerine, belirlediğimiz bir örnek PDF şablonu üzerine belirli alanları otomatik olarak veri kaynağından çekerek dolduracak bir yapı olmalı. Ayrıca, dosyaya erişimi kolaylaştırmak için PDF içinde bir QR kod da bulunmalı.
- 21-02-2025, 01:39:52Istemis oldugunuz yazilimi yapabilirim. Daha once PDF sablonlari uzerinde calismalarim oldu. Ornekleride gonderebilirim. Bana Detaylari pm den iletirseniz yardimci olurumReyhon adlı üyeden alıntı: mesajı görüntüle
- 21-02-2025, 01:43:42<?php
require_once('tcpdf/tcpdf.php');
require_once('phpqrcode/qrlib.php');
class PDFGenerator extends TCPDF {
private $templateData;
public function __construct($templateData) {
parent::__construct(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$this->templateData = $templateData;
}
public function generatePDF() {
// PDF döküman ayarları
$this->SetCreator('Your Company');
$this->SetAuthor('System');
$this->SetTitle('Generated Document');
// Sayfa ayarları
$this->AddPage();
$this->SetFont('dejavusans', '', 10);
// Şablon verilerini yerleştir
$this->placeTemplateData();
// QR kod oluştur ve yerleştir
$this->generateAndPlaceQRCode();
// PDF'i kaydet
return $this->Output('generated_document.pdf', 'S');
}
private function placeTemplateData() {
// Başlık
$this->SetFont('dejavusans', 'B', 16);
$this->Cell(0, 10, $this->templateData['title'], 0, 1, 'C');
// İçerik alanları
$this->SetFont('dejavusans', '', 10);
$y = 50;
foreach ($this->templateData['fields'] as $label => $value) {
$this->SetXY(20, $y);
$this->Cell(50, 10, $label . ':', 0);
$this->SetXY(70, $y);
$this->Cell(0, 10, $value, 0);
$y += 10;
}
}
private function generateAndPlaceQRCode() {
// QR kod için benzersiz tanımlayıcı oluştur
$documentId = uniqid();
$qrData = "https://yourcompany.com/verify/" . $documentId;
// Geçici QR kod dosyası oluştur
$tempDir = sys_get_temp_dir();
$qrFile = $tempDir . '/qr_' . $documentId . '.png';
QRcode:
ng($qrData, $qrFile, QR_ECLEVEL_L, 3);
// QR kodu PDF'e yerleştir
$this->Image($qrFile, 160, 20, 30);
// Geçici dosyayı sil
unlink($qrFile);
}
}
// Kullanım örneği
function createDocumentFromData($data) {
// Veritabanından veri çekme örneği
$db = new PDO("mysql:host=localhost;dbname=your_database", "username", "password");
$stmt = $db->prepare("SELECT * FROM documents WHERE id = ?");
$stmt->execute([$data['id']]);
$documentData = $stmt->fetch(PDO::FETCH_ASSOC);
// Şablon verilerini hazırla
$templateData = [
'title' => 'Belge Başlığı',
'fields' => [
'Ad Soyad' => $documentData['full_name'],
'TC Kimlik No' => $documentData['identity_number'],
'Belge No' => $documentData['document_number'],
'Düzenleme Tarihi' => $documentData['issue_date'],
// Diğer alanlar...
]
];
// PDF oluştur
$pdfGenerator = new PDFGenerator($templateData);
$pdfContent = $pdfGenerator->generatePDF();
return $pdfContent;
}
// API endpoint örneği
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$requestData = json_decode(file_get_contents('php://input'), true);
$pdfContent = createDocumentFromData($requestData);
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="document.pdf"');
echo $pdfContent;
}
?>
Size PHP ile otomatik PDF oluşturma sistemi için bir çözüm sunacağım. TCPDF kütüphanesi kullanarak PDF oluşturacağız ve QR kod için de PHP QR Code kütüphanesinden yararlanacağız.
```php
<?php
require_once('tcpdf/tcpdf.php');
require_once('phpqrcode/qrlib.php');
class PDFGenerator extends TCPDF {
private $templateData;
public function __construct($templateData) {
parent::__construct(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$this->templateData = $templateData;
}
public function generatePDF() {
// PDF döküman ayarları
$this->SetCreator('Your Company');
$this->SetAuthor('System');
$this->SetTitle('Generated Document');
// Sayfa ayarları
$this->AddPage();
$this->SetFont('dejavusans', '', 10);
// Şablon verilerini yerleştir
$this->placeTemplateData();
// QR kod oluştur ve yerleştir
$this->generateAndPlaceQRCode();
// PDF'i kaydet
return $this->Output('generated_document.pdf', 'S');
}
private function placeTemplateData() {
// Başlık
$this->SetFont('dejavusans', 'B', 16);
$this->Cell(0, 10, $this->templateData['title'], 0, 1, 'C');
// İçerik alanları
$this->SetFont('dejavusans', '', 10);
$y = 50;
foreach ($this->templateData['fields'] as $label => $value) {
$this->SetXY(20, $y);
$this->Cell(50, 10, $label . ':', 0);
$this->SetXY(70, $y);
$this->Cell(0, 10, $value, 0);
$y += 10;
}
}
private function generateAndPlaceQRCode() {
// QR kod için benzersiz tanımlayıcı oluştur
$documentId = uniqid();
$qrData = "https://yourcompany.com/verify/" . $documentId;
// Geçici QR kod dosyası oluştur
$tempDir = sys_get_temp_dir();
$qrFile = $tempDir . '/qr_' . $documentId . '.png';
QRcode:
ng($qrData, $qrFile, QR_ECLEVEL_L, 3);
// QR kodu PDF'e yerleştir
$this->Image($qrFile, 160, 20, 30);
// Geçici dosyayı sil
unlink($qrFile);
}
}
// Kullanım örneği
function createDocumentFromData($data) {
// Veritabanından veri çekme örneği
$db = new PDO("mysql:host=localhost;dbname=your_database", "username", "password");
$stmt = $db->prepare("SELECT * FROM documents WHERE id = ?");
$stmt->execute([$data['id']]);
$documentData = $stmt->fetch(PDO::FETCH_ASSOC);
// Şablon verilerini hazırla
$templateData = [
'title' => 'Belge Başlığı',
'fields' => [
'Ad Soyad' => $documentData['full_name'],
'TC Kimlik No' => $documentData['identity_number'],
'Belge No' => $documentData['document_number'],
'Düzenleme Tarihi' => $documentData['issue_date'],
// Diğer alanlar...
]
];
// PDF oluştur
$pdfGenerator = new PDFGenerator($templateData);
$pdfContent = $pdfGenerator->generatePDF();
return $pdfContent;
}
// API endpoint örneği
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$requestData = json_decode(file_get_contents('php://input'), true);
$pdfContent = createDocumentFromData($requestData);
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="document.pdf"');
echo $pdfContent;
}
?>
```
Bu sistem şunları yapabilir:
1. Veritabanından veri çeker
2. Önceden tanımlanmış bir şablona göre PDF oluşturur
3. Her belge için benzersiz QR kod üretir
4. Türkçe karakter desteği için dejavusans fontu kullanır
5. Belgeleri dinamik olarak oluşturur ve indirilmesini sağlar
Sistemi kullanmak için:
1. TCPDF ve PHP QR Code kütüphanelerini kurmanız gerekiyor:
```bash
composer require tecnickcom/tcpdf
composer require phpqrcode/phpqrcode
```
2. Veritabanı bağlantı bilgilerinizi güncellemelisiniz
3. Şablon verilerini (`templateData`) kendi ihtiyaçlarınıza göre düzenleyebilirsiniz
4. QR kod URL'sini kendi doğrulama sisteminize göre ayarlayabilirsiniz
İsterseniz şu özelliklerle sistemi geliştirebilirim:
- Çoklu sayfa desteği
- Farklı şablon tipleri
- Özel font desteği
- PDF şifreleme
- Farklı veri kaynakları desteği
Claude ai ile yazıldı
Düzenleyip kullanabilirsiniz - 21-02-2025, 07:54:45Merhaba. Composera erişiminiz var mı?
Veri kaynağı olarak veri tabanı mı yoksa parametreli link mı?
Buna ek olarak pdf oluşturucu içine get parametresi eklemek mantıklı olabilir, isteğe bağlı veri id ye göre yapıp kaydetmek için, daha sonra da aynı pdf tekrar oluşturmamak için localden alıp hem tekil oluşturmayı hem toplu oluşturmayı çözmüş olursunuz.
Yapay zeka var olan yapıyı baz almamış olabilir (assumption problem)
ng($qrData, $qrFile, QR_ECLEVEL_L, 3);