PDF Decode Base64

Asked

Viewed 956 times

4

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

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

I tried to look for something but I didn’t find anything that clears my doubt.

My table is getting 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 exit was this:

inserir a descrição da imagem aqui

DOWNLOAD THE BASE64 PDF

  • Maybe I need to do the base64_decode() and then just format the upload header

  • Directly there is no way to do it, as in the example I added @rray

  • A suggestion is instead to imprint the pdf, put the link to a php (new) file that makes the query at the base takes the content and formats the header to pdf.

  • 2

    Of curiosity, what makes someone save something in Base64 in DB? I see a lot of people doing this, I wanted to understand where this strange idea comes from. Zero benefit, and waste of space.

  • 1

    It would only make sense to use Base64 if it was to send via a text protocol... Accessing via JSON or XML is practical like this.

  • The idea is to save in a database, as it would with XML ?

  • The file that sends the PDF is in the folder ajax/ but make download via AJAX is complicated and I do not recommend, a direct link to download page will open the save file window and the current page remains loaded. Saving the file on base 64 increases the size by approximately 33%, according to Wikipedia, you can use columns Blob in the database, avoiding the need for decoding.

Show 2 more comments

2 answers

4

I believe it is better to create a new php file, to generate the pdf, basically its content is a query to the desired record, correct header formatting for the browser enteder that this output(plain text) is a donwload 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>

gerarPDF.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'];
  • Please avoid long discussions in the comments; your talk was moved to the chat

  • @rray Still not able to solve the problem, now it is saving normally in mysql in the BLOB field, but still in the preview is not working, could help me back in this case ?

0

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.