PDF decode base64

4

I'm setting up a job where I need to download the file in pdf , however this file is saved in the database it is in base64 format.

My idea is to do when the comrade clicks on the line he does the download , of the file.

I tried to look for something but I did not find anything that heals my doubts.

My table looks like this:

echo '<tr>'
    . '<td><a class="ajax-link" href="ajax/gerapdf.php?id=' . $registro->DAT_PUBLIC_DOWNL . '">' . $registro->DAT_PUBLIC_DOWNL . '</a></td>'
    . '<td>' . $registro->TXT_TITUL_DOWNL . '</td>'
    . '<td>' . $registro->TXT_EXTEN_ARQUI . '</td>'
   . '</tr>';

gerapdf.php

<?php
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='arquivo.pdf'");

include '../includes/suc_validacao.php';
include '../includes/conexao.php';

$id = !empty($_GET['id']) ? $_GET['id'] : 0;

$conexao = new ConexaoDatabase();

$sql = "SELECT BLO_PDFXX_ARQUI  FROM DB_EGLISE.tbl_DOWNLOADS WHERE COD_IDENT_IGREJ = :igj and DAT_PUBLIC_DOWNL = :data";

$sqlVars = array();
$sqlVars[':igj'] = $suc->getCOD_IDENT_IGREJ();
$sqlVars[':data'] = $id;

$registros = $conexao->fetch($sql, $sqlVars);

echo base64_decode($registros->BLO_PDFXX_ARQUI);

The output was:

PDF BASE64 DOWNLOAD

    
asked by anonymous 08.03.2016 / 18:42

2 answers

4

I think it is better to create a new php file, to generate the pdf, basically the content of it is a query to the desired record, correct formatting of the header to the browser enteder that this output (pure text) is a download of a pdf .

In the listing file your link should look something like this:

<a href="gerarPDF.php?id=<?php echo $row['id'];?>">DOWNLOAD</a>

generatePDF.php

<?php
   $id = !empty($_GET['id']) ? $_GET['id'] : 0;

   $db = new PDO('mysql:host=localhost;dbname=teste', 'user', 'pass');
   $stmt = $db->prepare("SELECT * FROM tabela where id = ?");
   if(!$stmt->execute(array($id))){
      print_r($stmt->errorInfo());
   }

   $item = $stmt->fetch(PDO::FETCH_ASSOC);

   header("Content-type:application/pdf");
   header("Content-Disposition:attachment;filename='arquivo.pdf'");
   echo $item['BLO_PDFXX_ARQUI'];
    
08.03.2016 / 18:59
0

For your code, php would force a download, even if the pdf was corrupted, I believe your problem is "ajax-link". If you try to get content using ajax, you will have to raw pdf content. Remove the ajax-link class, put the direct link, I think it will work like this.

    
29.03.2016 / 04:48