Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    10
    Rep Power
    0

    Save content pdf to database and retrieve it


    Hi everybody...I'm a serious problem with my code.I need to save the content or the file pdf with the content into database.
    I use FPDF to generate my content and I save it like MEDIUMBLOB.
    I don't want to save the pdf file into diretory system.

    I want to know how to retrieve the content and put into pdf file?


    Thanks
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    the first question is: Why are you doing it like that? Why do you stuff the binary data of the file into the database just to write it back to a file?

    Without this detour, you obviously wouldn't even have the problem.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4124
    store files on the file system and references to it in the database.

    If you don't want your files to be web accessible then save them outside the web root, then use a php script to serve them to the appropriate user(s)
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    10
    Rep Power
    0
    Hi everyone,

    I don't want to save the pdf into file system because my site consists to sell products and when the buyer saw the paypal page, will generate automatically a pdf file or content of pdf into database. Later, the administrator will send an email to the customer with the pdf file.

    I show you how I generate my content of pdf and send him into dabatase:

    $query1 = "SELECT nome,apelido,email FROM user WHERE id = ?";
    $stmt1 = $mybd->prepare($query1);
    $stmt1->bind_param("i",$idUser);
    $stmt1->execute();
    $stmt1->bind_result($nomeUser,$apelido,$email);
    $stmt1->fetch();
    $stmt1->close();
    if(isset($_GET["idVoucher"])){
    $id = $_GET["idVoucher"];
    $query = "SELECT v.titulo,v.descricao, v.precoOriginal, v.desconto,v.link,c.nome, i.nome
    FROM voucher as v
    LEFT JOIN menu as m on m.idVoucher = v.id
    LEFT JOIN categoria as c on v.id_categoria = c.id
    LEFT JOIN imagem as i on v.id = i.id_Voucher
    WHERE v.id = ?";
    $stmt = $mybd->prepare($query);
    $stmt->bind_param("i",$id);
    $stmt->execute();
    $stmt->bind_result($titulo,$descricao,$precoOriginal,$desconto,$link,$nomeCategoria,$nomeImagem);

    $stmt->fetch();

    $precoAtual = round(($precoOriginal * $desconto) / 100);
    $poupa = round($precoOriginal - $precoAtual);
    $hiperlink = mysqli_real_escape_string($mybd,urldecode($link));

    }
    $stmt->close();
    $nomePDF = "voucher" . $id;
    $query2 = "SELECT m.nome,m.descricao
    FROM menu as m
    LEFT JOIN voucher as v on v.id = m.idVoucher
    WHERE v.id = ?";
    $stmt2 = $mybd->prepare($query2);
    $stmt2->bind_param("i",$id);
    $stmt2->execute();
    $stmt2->bind_result($nomeMenu,$descricaoMenu);
    $tudo = array();
    $menus = array();
    $descricoes = array();
    while($stmt2->fetch()){
    $descricoes[] = $descricaoMenu;
    $menus[] = $nomeMenu;
    }
    $stmt2->close();



    class FPDF extends tFPDF
    {
    function Header()
    {
    $hoje = date("d/m/y");
    $this->Cell(10,10,"$hoje",0,'L');
    $this->Image('./images/logo.png',170,10,30);
    $this->SetFont('Arial','',9);
    $this->Cell(70);
    $this->Cell(30,10,'Nome da empresa',0,'C');
    $this->Ln(15);
    }

    function NomeComprador($nomeUser,$apelido,$nomeCategoria)
    {
    $this->SetFont('DejaVu','',10);
    $this->Cell(0,4,"Nome do Utilizador: $nomeUser" . " $apelido",0,1,'C');
    $this->Cell(-1);
    $this->Cell(0,5,"Categoria: $nomeCategoria",0,1,'C');
    $this->Ln(4);
    }

    function informacaoImagem($nomeImagem,$titulo,$descricao,$poupa,$desconto,$quantidade)
    {
    $this->nomeImagem = $nomeImagem;
    $total = $quantidade * $poupa;
    $this->Image('./images/paginaPrincipal/' . $this->nomeImagem,30,50,50);
    $this->Cell(125,30,"Nome do Voucher: $titulo",0,1,'R');
    $this->Cell(120.5,-15,"Descrição: $descricao",0,1,'R');
    $this->SetFont('DejaVu','B',22);
    $this->Cell(180,40,"$total" . " €",0,1,'C');
    $this->SetFont('DejaVu','',11);
    $this->Cell(250,-45,"Desconto: $desconto" . '%',0,1,'C');
    }

    function informacaoOferta($menus,$descricoes)
    {

    foreach ($menus as $key => $value) {
    foreach ($descricoes as $key2 => $value2){
    if($key == 0 && $key2 == 0){
    $valor = strip_tags($value2);
    $this->SetFont('DejaVu','B',14);
    $this->Cell(125,100,"$value:",0,1,'L');
    $this->SetFont('DejaVu','','11');
    $this->Write(5,$this->Cell(125,-75,"$valor"));
    $this->Ln(10);
    }elseif ($key == 1 && $key2 == 1){
    $valorFinal = strip_tags($value2);
    $this->SetFont('DejaVu','B',13);
    $this->Cell(125,-30,"$value:",0,1,'L');
    $this->SetFont('DejaVu','','11');
    $this->Write(5,"$valorFinal");
    $this->Ln(50);
    }elseif($key == 2 && $key2 == 2){
    $valor3 = strip_tags($value2);
    $this->SetFont('DejaVu','B',13);
    $this->Cell(125,-50,"$value:",0,1,'L');
    $this->SetFont('DejaVu','','11');
    $this->Write(7,"$valor3");
    $this->Ln(30);
    }elseif($key == 3 && $key2 == 3){
    $valor4 = strip_tags($value2);
    $this->SetFont('DejaVu','B',13);
    $this->Cell(125,-10,"$value:",0,1,'L');
    $this->SetFont('DejaVu','','11');
    $this->Write(3,"$valor4");
    $this->Ln(40);
    }elseif($key == 4 && $key2 == 4){
    $valor5 = strip_tags($value2);
    $this->SetFont('DejaVu','B',13);
    $this->Cell(125,20,"$value:",0,1,'L');
    $this->SetFont('DejaVu','','11');
    $this->Write(0,"$valor5");
    $this->Ln(20);
    }
    }
    }
    }

    function EAN13($x, $y, $barcode, $h=16, $w=.35)
    {
    $this->Barcode($x,$y,$barcode,$h,$w,13);
    }

    function UPC_A($x, $y, $barcode, $h=16, $w=.35)
    {
    $this->Barcode($x,$y,$barcode,$h,$w,12);
    }

    function GetCheckDigit($barcode)
    {
    $sum=0;
    for($i=1;$i<=11;$i+=2)
    $sum+=3*$barcode[$i];
    for($i=0;$i<=10;$i+=2)
    $sum+=$barcode[$i];
    $r=$sum%10;
    if($r>0)
    $r=10-$r;
    return $r;
    }

    function TestCheckDigit($barcode)
    {
    $sum=0;
    for($i=1;$i<=11;$i+=2)
    $sum+=3*$barcode[$i];
    for($i=0;$i<=10;$i+=2)
    $sum+=$barcode[$i];
    return ($sum+$barcode[12])%10==0;
    }

    function Barcode($x, $y, $barcode, $h, $w, $len)
    {
    $barcode=str_pad($barcode,$len-1,'0',STR_PAD_LEFT);
    if($len==12)
    $barcode='0'.$barcode;

    if(strlen($barcode)==12)
    $barcode.=$this->GetCheckDigit($barcode);
    elseif(!$this->TestCheckDigit($barcode))
    $this->Error('Incorrect check digit');

    $codes=array(
    'A'=>array(
    '0'=>'0001101','1'=>'0011001','2'=>'0010011','3'=>'0111101','4'=>'0100011',
    '5'=>'0110001','6'=>'0101111','7'=>'0111011','8'=>'0110111','9'=>'0001011'),
    'B'=>array(
    '0'=>'0100111','1'=>'0110011','2'=>'0011011','3'=>'0100001','4'=>'0011101',
    '5'=>'0111001','6'=>'0000101','7'=>'0010001','8'=>'0001001','9'=>'0010111'),
    'C'=>array(
    '0'=>'1110010','1'=>'1100110','2'=>'1101100','3'=>'1000010','4'=>'1011100',
    '5'=>'1001110','6'=>'1010000','7'=>'1000100','8'=>'1001000','9'=>'1110100')
    );
    $parities=array(
    '0'=>array('A','A','A','A','A','A'),
    '1'=>array('A','A','B','A','B','B'),
    '2'=>array('A','A','B','B','A','B'),
    '3'=>array('A','A','B','B','B','A'),
    '4'=>array('A','B','A','A','B','B'),
    '5'=>array('A','B','B','A','A','B'),
    '6'=>array('A','B','B','B','A','A'),
    '7'=>array('A','B','A','B','A','B'),
    '8'=>array('A','B','A','B','B','A'),
    '9'=>array('A','B','B','A','B','A')
    );
    $code='101';
    $p=$parities[$barcode[0]];
    for($i=1;$i<=6;$i++)
    $code.=$codes[$p[$i-1]][$barcode[$i]];
    $code.='01010';
    for($i=7;$i<=12;$i++)
    $code.=$codes['C'][$barcode[$i]];
    $code.='101';

    for($i=0;$i<strlen($code);$i++)
    {
    if($code[$i]=='1')
    $this->Rect($x+$i*$w,$y,$w,$h,'F');
    }
    $this->SetFont('Arial','',12);
    $this->Text($x,$y+$h+11/$this->k,substr($barcode,-$len));
    }

    function PrintComprador($nomeUser,$apelido,$nomeCategoria,$titulo,$nomeImagem,$descricao,$poupa,$desconto,$qu antidade)
    {
    $this->AddPage();
    $this->NomeComprador($nomeUser,$apelido,$nomeCategoria);
    $this->informacaoImagem($nomeImagem,$titulo,$descricao,$poupa,$desconto,$quantidade);
    }

    function PrintOferta($menus,$descricoes)
    {
    $this->informacaoOferta($menus,$descricoes);
    }

    function Footer()
    {
    $this->SetY(-15);
    $this->SetFont('DejaVu','',8);
    $this->Cell(0,10,'Página '.$this->PageNo().'/{nb}',0,0,'C');
    }
    }
    $pdf = new FPDF();
    $pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true);
    $pdf->AddFont('DejaVu','B','DejaVuSansCondensed-Bold.ttf',true);
    $pdf->SetFont('DejaVu','',14);
    $pdf->AliasNbPages();
    for($i = 0; $i < $quantidade; $i++){
    $pdf->PrintComprador($nomeUser,$apelido,$nomeCategoria,$titulo,$nomeImagem,$descricao,$poupa,$desconto,$q uantidade);
    $random = substr(number_format(time() * rand(),0,'',''),0,12);
    $pdf->EAN13(40,20,$random);
    $pdf->PrintOferta($menus,$descricoes);
    $pdf->Write(40,"Para descobrir a localização, descubra ");
    $pdf->SetFont('','U');
    $link = $pdf->AddLink();
    $pdf->Write(40,'aqui',$hiperlink);
    $pdf->SetFont('DejaVu','B',12);
    $pdf->Ln(2);
    $pdf->Write(70,"Procedimento:");
    $pdf->Ln(2);
    $pdf->SetFont('DejaVu','',11);
    $pdf->Write(90,"1- Imprimir o cupão.");
    $pdf->Ln(2);
    $pdf->SetFont('DejaVu','',11);
    $pdf->Write(110,"2- Leia atentamente as condições e os detalhes da compra.");
    $pdf->Ln(2);
    $pdf->SetFont('DejaVu','',11);
    $pdf->Write(130,"3- Apresentação do cupão no estabelecimento previsto.");
    }

    //generate my PDF content (see how I insert into database at the end of the file)
    $file = $pdf->Output("","S");



    How to insert into database:

    $query = "SELECT nome,email FROM user WHERE id = ?";
    $stmt = $mybd->prepare($query);
    $stmt->bind_param("i",$idUser);
    $stmt->execute();
    $stmt->bind_result($nomeUser,$email);
    $stmt->fetch();
    $stmt->close();

    //Insert into database.
    $query = "INSERT INTO historico (nomeUser,email,nomeVoucher,categoria,quantidade,preco,confirmacao,fileName,filePDF,idUser) VALUES (?,?,?,?,?,?,?,?,?,?)";
    $stmt = $mybd->prepare($query) or die("Erro na base de dados");
    $stmt->bind_param("ssssiisssi",$nomeUser,$email,$titulo,$nomeCategoria,$quantidade,$total,$confirmacao,$fi le2,$ficheiro,$idUser);
    $stmt->execute();
    $stmt->close();


    It's possible to save the content directly into pdf file and send it to database?
  8. #5
  9. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4124
    Originally Posted by jonito
    I don't want to save the pdf into file system
    I don't care what you want or don't want. There's a right way and a wrong way to do this.

    If you choose the right way, you'll receive much more help than if you choose the wrong way

    The right way is to store files on the file system
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    10
    Rep Power
    0
    So you tell me there's no way to save content of the pdf?
    I know storing pdf file into file system is the best way but, in my case, I don't need this because is unnecessary. I don't want to overload my site with thousands of pdf files.

    Just that...If there's no way to store content of pdf file like I want then I will use your methods

    Thanks

    Jonito
  12. #7
  13. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4124
    I'm sure there is a way but in over 10 years I've never tried or had to because it is unnecessary as I have a file system to store files in and a database to store data in.
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    10
    Rep Power
    0
    I understand your point and I agree about what you said but my client wants the way what I need
  16. #9
  17. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    file_get_contents()

    file_put_contents()

    Is using Google really so hard?
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    10
    Rep Power
    0
    Well Jacques1, I using this two functions and works find but the function file_put_content() save the pdf into my diretory where I have the others file. And like I said, I don't want to save it into diretory...Just send into database and retrieve it.
  20. #11
  21. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    You asked us several times how to retrieve the data from the database and save it in a PDF file.

    But appearently, that's not what you want. So what is it what you want? Do you wanna inject the data directly into the mail? Then this is (obviously) a matter of your mail code and how it handles attachments. PHPMailer , for example, has the AddStringAttachment() method, which generates an attached file from a data string. If you're fumbling with low-level functions like mail(), you have to stuff the data into the headers.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    10
    Rep Power
    0
    Well I'm using phpMailer to try to send the pdf file to the user.

    In my database, I send just the content when i generate with FPDF.
    But If i'm using addStringAttachment, I need a pdf file or not?
  24. #13
  25. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by jonito
    But If i'm using addStringAttachment, I need a pdf file or not?
    No, the method takes a string of data -- hence the name.

    The manual, section "String Attachments"
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    10
    Rep Power
    0
    I will try and tell you something.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    10
    Rep Power
    0
    It works but not entirely....Before sending to database I use mysql_real_string_escape for the content of my pdf...At this moment I have a blob data with 164 KB...When I send an email, the user receive just 1 KB.

    I don't know why
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo