Generate pdfs

Hello! This is not a error report or a bug, I’m looking for advice.

I’ve been reading some posts but I don’t find a clear answer, some are old and things might have changed.

As per today what would be the best method (if any) to create pdf’s from infinityfree free sites?
I have an application where some users upload worksheets and most of them are pdf, but some are in jpeg format. If like to standardize then to pdf on upload.

Thanks!

usually i would recommend imagemagik but its not installed here

there is this website that has a free api for converting images to pdf but they only allow 100 per month

probably someone else will have a better solution

5 Likes

Thanks, I prefer not to use external services if possible. Any “in house” solution? Thanks!!

im sure theres probably a simple way to create a pdf wrapper for the image using php , if I find something i’ll post here

If I cant find anything i’ll try that free website and examine its generated output file which might give enough clues for me to write a script in php

2 Likes

Thanks, I’m checking the options using GD library that’s pre-installed on infinityfree together with tcpdf.
I’ll report back once in on my pc. (Mobile now, holy vacations!)

Take note pdf files are not allowed on free hosting

4 Likes

oh what a bummer :frowning:

1 Like

Yup

This thread will provide more info.

I believe you can try to generate pdf files but these files cannot be stored on free hosting

4 Likes

Well that is a bummer, I don’t agree that pdf files generated and used by the website might suppose a problem for the host
Maybe someone that uses a free hosting for storing piracy ebooks in pdf might be an issue, but files generated by the system and used by it… No sense.

Maybe it’s just a way to push people to the paid services (which I can even understand, but if said openly)

for others with the same issue, here is a basic code generated by chatgpt (hey, I dont know Sh*t about coding! we live in wonderful times… :smiley: ) that works after a few tweaks:

File structure:

index.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image to PDF Converter</title>
</head>
<body>
    <form action="upload.php" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
    </form>
</body>
</html>

upload.php:


<?php
require_once('tcpdf/tcpdf.php');

$target_dir = "uploads/";



$datestamp = date("Y-m-d_H-i-s");



$filename = $datestamp . '--' . str_replace(' ', '_', basename($_FILES["fileToUpload"]["name"]));
$target_file = $target_dir . $filename;

$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

// Check if image file is an actual image or fake image
if (isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if ($check !== false) {
        $uploadOk = 1;
    } else {
        echo "Error: File is not an image.";
        $uploadOk = 0;
    }
}

// Check if file already exists
if (file_exists($target_file)) {
    echo "Error: File already exists.";
    $uploadOk = 0;
}

// Check file size (optional)
// if ($_FILES["fileToUpload"]["size"] > 500000) {
//     echo "Error: Your file is too large.";
//     $uploadOk = 0;
// }

// Allow certain file formats
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
    echo "Error: Only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
    exit(); // Terminate script execution
}

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file " . basename($_FILES["fileToUpload"]["name"]) . " has been uploaded.";

    // Standard A4 dimensions in mm
    $a4_width = 210;
    $a4_height = 297;

    // Standard printing margins in mm
    $margin = 10;

    // Calculate available width and height on A4 page after considering margins
    $available_width = $a4_width - 2 * $margin;
    $available_height = $a4_height - 2 * $margin;

    // Check the image dimensions to determine PDF orientation
    list($width, $height) = getimagesize($target_file);
    $image_aspect_ratio = $width / $height;
    $a4_aspect_ratio = $available_width / $available_height;

    $orientation = ($image_aspect_ratio > $a4_aspect_ratio) ? 'L' : 'P';

    // Convert the image to PDF
    $pdf = new TCPDF($orientation, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    $pdf->AddPage();

    // Adjust image size to fit the available space on the PDF page
    if ($orientation === 'L') {
        // Swap the A4 dimensions for landscape orientation
        $temp = $available_width;
        $available_width = $available_height;
        $available_height = $temp;
    }

    // Calculate the dimensions to scale the image while maintaining its aspect ratio
    if ($image_aspect_ratio > $a4_aspect_ratio) {
        // Image width will fill the available width
        $scaled_width = $available_width;
        $scaled_height = $scaled_width / $image_aspect_ratio;
    } else {
        // Image height will fill the available height
        $scaled_height = $available_height;
        $scaled_width = $scaled_height * $image_aspect_ratio;
    }

    $pdf->Image($target_file, $margin, $margin, $scaled_width, $scaled_height);
    $pdf->Output(__DIR__ . '/' . $target_file . '.pdf', 'F');

    echo " and converted to PDF.";
} else {
    echo "Error: There was an error uploading your file.";
}
?>

let’s see for how long I can get the pdf on my free account before getting a warning. I’m not doing anything ilegal, just taking my pictures and uploading them, a few megabytes.
EDIT: I’m looking on theoption to generate the pdfon my website hosted in infinifyfree but storing it on amazon S3, seems to be cheap

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.