<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>File to Google Docs Viewer Converter</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            padding: 20px;
            max-width: 800px;
            margin: 0 auto;
        }
        h1 {
            color: #333;
            text-align: center;
        }
        label {
            display: block;
            margin-top: 10px;
            color: #555;
        }
        input {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
            margin-top: 5px;
        }
        button {
            margin-top: 10px;
            padding: 10px;
            cursor: pointer;
            background-color: #007bff;
            color: #fff;
            border: none;
        }
        .result {
            margin-top: 20px;
        }
        p {
            color: #555;
            margin-bottom: 5px;
        }
        textarea {
            width: 100%;
            height: 100px;
            margin-top: 10px;
            resize: none;
            padding: 8px;
            box-sizing: border-box;
        }
        .file-preview {
            margin-top: 20px;
            border: 1px solid #ccc;
            padding: 10px;
        }
        img {
            max-width: 100%;
            height: auto;
        }
        .copy-btn {
            display: none;
            background-color: #28a745;
            color: #fff;
            padding: 8px 12px;
            border: none;
            cursor: pointer;
        }
        iframe {
            width: 100%;
            height: 400px;
            margin-top: 20px;
            border: 1px solid #ccc;
        }
    </style>
</head>
<body>

    <h1>File to Google Docs Viewer Converter</h1>
    
    <label for="fileLink">File URL:</label>
    <input type="text" id="fileLink" placeholder="Enter file URL">

    <button onclick="convertLinks()">Generate Links</button>

    <div class="result">
        <h2>Converted Links:</h2>
        <p id="gviewLink"><strong>Google Docs Viewer Link:</strong> <a href="#" target="_blank"></a></p>
        <p><strong>Embed Code:</strong></p>
        <textarea id="embedCodeTextarea" readonly></textarea>

        <div class="copy-btn" onclick="copyToClipboard()">Copy</div>
    </div>
 
    <iframe id="previewIframe" frameborder="0" scrolling="no"></iframe>

    <script>
        function convertLinks() {
            const fileLink = document.getElementById('fileLink').value.trim();

            if (!fileLink) {
                alert('Please enter a file URL.');
                return;
            }

            const gviewLink = `https://docs.google.com/gview?url=${fileLink}`;
            const embedCode = `<iframe src="${gviewLink+"&embedded=true"}" width="600" height="780" frameborder="0" scrolling="no"></iframe>`;

            document.getElementById('gviewLink').innerHTML = `<strong>Google Docs Viewer Link:</strong> <a href="${gviewLink}" target="_blank">${gviewLink}</a>`;
            document.getElementById('embedCodeTextarea').value = embedCode;

            // Display copy button
            document.querySelector('.copy-btn').style.display = 'inline-block';

            // Display file preview
            const fileExtension = fileLink.split('.').pop().toLowerCase();
            const previewContainer = document.getElementById('filePreview');
            const previewImage = document.getElementById('previewImage');
            const previewIframe = document.getElementById('previewIframe');

            if (fileExtension === 'jpg' || fileExtension === 'jpeg' || fileExtension === 'png' || fileExtension === 'gif') {
                previewImage.src = fileLink;
                previewImage.style.display = 'block';
                previewIframe.style.display = 'none';
            } else {
                previewImage.style.display = 'none';
                previewContainer.style.display = 'none';
                previewIframe.src = gviewLink+"&embedded=true";
                previewIframe.style.display = 'block';
            }
        }

        function copyToClipboard() {
            const textarea = document.getElementById('embedCodeTextarea');
            textarea.select();
            document.execCommand('copy');
            alert('Embed code copied to clipboard!');
        }
    </script>

</body>
</html>
// Article Link :- https://dev.to/sh20raj/google-docs-viewer-viewembed-various-file-types-directly-in-their-browser-without-the-need-for-downloading-59p8

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.