John Davidson

php - Read PDF File into html not browser

0 comments
Message:


I want to display .pdf file content in my php website. I already tried with a simple code like this:


#[Route('/pdf', name: 'app_pdf')]
public function pdfAction()
{
$path = $this->getParameter('kernel.project_dir').'\public\build\images\tracemonkey.pdf';

$response = new BinaryFileResponse($path);

$response->headers->set('Content-Type', 'application/pdf');
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_INLINE,
'tracemonkey.pdf'
);

return $response;
}

Its working fine but it showing me the pdf in browser so my question is: It is possible to know how to display a pdf not in the browser but in an html tag? (Here in my p.pdf)
How to read pdf file in my p.pdf ?




.container-news {
width: 100%;
background-color: white;
}
.container-news .news-header {
width: 100%;
margin: 15px auto;
text-align: left;
border-bottom: 1px solid red;
}
.container-news .news-header h1 {
font-size: 1.5rem;
margin: 0;
}
.container-news .news-body {
width: 100%;
display: flex;
justify-content: space-between;
}
.container-news .news-body .news-card {
width: 25%;
background-color: #f7f7f7;
margin: 5px 0;
padding: 5px;
}
.container-news .news-body .news-card .news-title h2 {
text-align: center;
font-size: 1.3rem;
margin: 0;
min-height: 40px;
line-height: 3rem;
}
.container-news .news-body .news-content h3 {
font-size: 0.8rem;
margin: 0;
}
.container-news .news-body .news-content a {
float: right;
}
.pdf {
min-height: 150px;
border: 1px solid red;
}

<div class="container-news">
<div class="news-header">
<h1>Dernière actualités</h1>
</div>

<div class="news-body">

<div class="news-card rounded">
<div class="news-title">
<h2>Cours SQL (lesson)</h2>
</div>
<div class="news-content">
<h3>Catégorie: <span class="alert-success">Informatique/Programmation</span></h3>
<p class="text-muted mt-4">SQL, pour Structured Query Language, est un langage qui permet d'interroger une base de données relationnelle afin de pouvoir modifier...</p>
<p class="pdf">SHOW PDF FILES HERE</p>
<a href="#">En voir plus</a>
</div>
</div>

<div class="news-card">
<div class="news-title">
<h2>Cours SQL (lesson)</h2>
</div>
<div class="news-content">
<h3>Catégorie: <span class="alert-success">Informatique/Programmation</span></h3>
<p class="text-muted mt-4">SQL, pour Structured Query Language, est un langage qui permet d'interroger une base de données relationnelle afin de pouvoir modifier...</p>
<p class="pdf">SHOW PDF FILES HERE</p>
<a href="#" class="">En voir plus</a>
</div>
</div>

<div class="news-card">
<div class="news-title">
<h2>Cours SQL (lesson)</h2>
</div>
<div class="news-content">
<h3>Catégorie: <span class="alert-success">Informatique/Programmation</span></h3>
<p class="text-muted mt-4">SQL, pour Structured Query Language, est un langage qui permet d'interroger une base de données relationnelle afin de pouvoir modifier...</p>
<p class="pdf">SHOW PDF FILES HERE</p>
<a href="#">En voir plus</a>
</div>
</div>

</div>
</div>


My Answer:
To display the content of a PDF file within an HTML tag, you can use a library like PDF.js. PDF.js is a JavaScript library that allows you to render PDF files using HTML5 and JavaScript.

Here's how you can use PDF.js to display a PDF file within a `
` tag in your HTML:

1. First, include the PDF.js library in your HTML file. You can either download the library and include it locally or use a CDN link. Here's an example using a CDN link:

html



2. Next, create a `` element within the `
` where you want to display the PDF file:

html





3. Finally, use JavaScript to load and render the PDF file using PDF.js:

javascript
// PDF.js script
const pdfUrl = 'path/to/your/pdf/file.pdf';
const pdfjsLib = window['pdfjs-dist/build/pdf'];

pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.8.335/pdf.worker.min.js';

pdfjsLib.getDocument(pdfUrl).promise.then(pdf => {
pdf.getPage(1).then(page => {
const canvas = document.getElementById('pdf-render');
const context = canvas.getContext('2d');

const viewport = page.getViewport({ scale: 1.5 });
canvas.height = viewport.height;
canvas.width = viewport.width;

page.render({
canvasContext: context,
viewport: viewport
});
});
});


Replace `'path/to/your/pdf/file.pdf'` with the actual path to your PDF file. This script will load the PDF file, render the first page on the canvas, and display it within the `
` with the class `pdf`.

Make sure to adjust the styles of the `` element and the containing `
` to fit your layout and design requirements.

With this setup, you should be able to display the content of a PDF file within an HTML tag on your website.

Rate this post

5 of 5 based on 8510 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us