Text to PDF Converter
Creating a responsive text-to-PDF converter involves setting up an HTML interface, styling it with CSS, and using JavaScript to generate and download the PDF. Below is a complete example of how to implement this functionality.
HTML (index.html)
```html
Text to PDF Converter
```
### CSS (styles.css)
```css
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
color: #333;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
text-align: center;
width: 90%;
max-width: 500px;
}
h1 {
color: #4a90e2;
margin-bottom: 20px;
}
textarea {
width: 100%;
height: 200px;
border: 2px solid #4a90e2;
border-radius: 5px;
padding: 10px;
font-size: 16px;
resize: none;
margin-bottom: 20px;
}
button {
background-color: #4a90e2;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #357ab8;
}
```
JavaScript (script.js)
```javascript
document.getElementById('generate-pdf').onclick = () => {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF();
const textInput = document.getElementById('text-input').value;
pdf.text(textInput, 10, 10);
pdf.save('download.pdf');
};
```
Explanation:
1. **HTML**: Sets up the structure for the text input area and the button to generate the PDF.
2. **CSS**: Provides colorful styling, making the interface visually appealing and responsive.
3. **JavaScript**: Utilizes the jsPDF library to convert the input text into a PDF file and triggers the download when the button is clicked.
Usage:
1. Save the code into three separate files (`index.html`, `styles.css`, and `script.js`).
2. Open `index.html` in a web browser.
3. Enter your text in the textarea and click the "Generate PDF" button to download your PDF.
### Note:
This implementation uses the jsPDF library, which can handle basic text input and formatting. Ensure you have internet access to load the jsPDF library from the CDN.
Comments
Post a Comment