Saving html page in local storage using php - php

I am using PDFTOHTML (a php library) to convert pdf files to html and it's working fine but it's showing converted file in a browser and not storing in local folder, i want to store converted html in local folder using php with the same name as pdf was i-e mydata.pdf to mydata.html
Code that is converting pdf to html is:-
<?php
// if you are using composer, just use this
include 'vendor/autoload.php';
$pdf = new \TonchikTm\PdfToHtml\Pdf('cv.pdf', [
'pdftohtml_path' => 'C:/wamp64/www/new/poppler-0.51/bin/pdftohtml.exe',
'pdfinfo_path' => 'C:/wamp64/www/new/poppler-0.51/bin/pdfinfo.exe'
]);
// get content from all pages and loop for they
foreach ($pdf->getHtml()->getAllPages() as $page) {
echo $page . '<br/>';
}
?>

Just change your foreach to
$filePdf = 'cv'; // your pdf filename without extension
$pdf = new \TonchikTm\PdfToHtml\Pdf($filePdf.'.pdf', [
'pdftohtml_path' => 'C:/wamp64/www/new/poppler-0.51/bin/pdftohtml.exe',
'pdfinfo_path' => 'C:/wamp64/www/new/poppler-0.51/bin/pdfinfo.exe'
]);
$counterPage = 1;
foreach ($pdf->getHtml()->getAllPages() as $page) {
$filename = $filePdf . "_" . $counterPage.'.html'; // set as string directory and filename where you want to save it
if (file_exists($filename)) {
// if file exist do something
} else {
// else
$fileOpen = fopen($filename, 'w+');
fputs($fileOpen, $page);
fclose($fileOpen);
}
$counterPage++;
echo $page . '<br/>';
}
This will create you file for example: example_1.html, example_2.html and so on.
if this not help you then probably you need to use file_put_contents with ob_start() and ob_get_contents() read more here

Look this :
<?php
// if you are using composer, just use this
include 'vendor/autoload.php';
$pdf = new \TonchikTm\PdfToHtml\Pdf('cv.pdf', ['pdftohtml_path' => 'C:/wamp64/www/new/poppler-0.51/bin/pdftohtml.exe', 'pdfinfo_path' => 'C:/wamp64/www/new/poppler-0.51/bin/pdfinfo.exe']);
// get content from all pages and loop for they
$file = fopen('cv.html', 'w+');
$data = null;
foreach ($pdf->getHtml()->getAllPages() as $page) {
$data .= "".$page."<br/>";
}
fputs($file, $data);
fclose($file);
I did not test this code

Related

FPDM Only first file output works

Acoording this thread I have a similar problem:
Only first pdf file filled with fpdm can be opened
With FPDM (https://github.com/codeshell/fpdm) even with the latest fix (https://gist.github.com/josh-candybox/173cacc476631720a05879327950da4e) I just can't get multiple pdf files processing. One file only. It is not header related, since the files are being thrown out as files (not as downloads).
See me code. One suggested to do the loop with an ajax call. If this is really the only way, how can I do that? I even try to reset the object/class. It just doesn't care...
Error msg: FPDF-Merge Error: getFilter cannot open stream of object
because filter '' is not supported, sorry.
$j=1;
foreach ($id as $value => $key) {
if ($value == 'adresse') {
echo $value." -> ".nl2br($key)."<br>\n";
$fields = array(
'adresse1' => $key
);
$pdf = NULL;
$pdf = new FPDM(__DIR__.'/fpdm/dmc3fixed.pdf' );
$pdf->Load($fields, true);
$pdf->Merge();
$filename=__DIR__."/fpdm/dmc".$j.".pdf";
$pdf->Output($filename,'F');
$pdf->closeFile();
unset($pdf);
$pdf = NULL;
$j++;
} else { ... }
P.S.: Kind of workaround, but doesn't answer my question:
So, if anyone of you has the same problem, actually I managed to accomplish generating multiple PDFs with dynamic text. In my case I wanted to put addresses to letter templates. So I made a PDF Form with a multi cell. I ended up just printing the address with FPDF and FPDI, so... here you go:
require_once __DIR__ . DIRECTORY_SEPARATOR .'fpdi'.DIRECTORY_SEPARATOR.'autoload.php';
require_once(__DIR__ . DIRECTORY_SEPARATOR .'fpdf'.DIRECTORY_SEPARATOR.'fpdf.php');
require_once(__DIR__ . DIRECTORY_SEPARATOR .'fpdi'.DIRECTORY_SEPARATOR.'fpdi.php');
use setasign\Fpdi\Fpdi;
$pdf = null;
$i = 1;
foreach ($result as $value => $key) {
$pdf = new FPDI();
$pagecount = $pdf->setSourceFile(__DIR__ . DIRECTORY_SEPARATOR.'template.pdf');
for ($n = 1; $n <= $pagecount; $n++) {
$pdf->AddPage();
$tplIdx = $pdf->importPage($n);
$pdf->useTemplate($tplIdx);
$pdf->SetFont('Arial', '', 11);
$pdf->SetXY(25, 60);
$pdf->MultiCell(80, 5, $address);
$pdf->Output(__DIR__ . DIRECTORY_SEPARATOR."output".$i.".PDF", "F");
$pdf = NULL;
$i++;
}
}

PHPExcel xlsx file into array ....Reader_Exception: Could not open

1.8.0"
the goal of my web application is to have a button that the user clicks on.
The code behind will start to scan all the folders that are in the main rdv folder.
in the rdv folder it contains three folders named 1000, 1001, 1002 these contain .xlsx files.
retrieves the last file .xlsx saved in each of the folders
but after that it doesn't work.
Fatal error: Uncaught PHPExcel_Reader_Exception: Could not open 2.xlsx
for reading!
so how i can fix the fatal error and how is possible to read the file to continue the code, i want to get 3 values in the .xlsx file and insert into my database.
thank you for your time.
include 'database.php';
require 'PHPExcel/Classes/PHPExcel.php';
require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
// receives the 'someAction' value from the index page button.
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['someAction']))
{
func();
}
function func()
{
//calcul the nomber of folder in folder rdv
$howManyFolder = count(glob('rdv/*', GLOB_ONLYDIR));
//retrieves the last file saved in each of the folders and insert into database
for ($i = 0; $i < $howManyFolder; $i++) {
$files = scandir("rdv/100$i", SCANDIR_SORT_DESCENDING);
echo "Recover all files in the folder 100" . $i . "<br>";
print_r($files ); echo "<br>";
echo "get the last file inserted in the folder at index 0 <br>";
$newest_file = $files[0]; print_r($newest_file); echo "<br>";
//load the file .xlsx (but the code bug here -_-)
$objExcel=PHPExcel_IOFactory::load($newest_file);
//get all value insinde the .xlsx file
$dossier = $objExcel->getActiveSheet()->getCell('A3')->getValue();
$facture = $objExcel->getActiveSheet()->getCell('B21')->getValue();
$date = $objExcel->getActiveSheet()->getCell('Q1')->getValue();
//call function to insert these values inside dataBase.
insertInto($dossier, $facture, $date );
}
}
XML Parser:
https://www.php.net/manual/en/book.xml.php
Parsing an XML document: https://www.php.net/manual/en/function.xml-parse.php
<?PHP
$stream = fopen('large.xml', 'r');
$parser = xml_parser_create();
// set up the handlers here
while (($data = fread($stream, 16384))) {
xml_parse($parser, $data); // parse the current chunk
}
xml_parse($parser, '', true); // finalize parsing
xml_parser_free($parser);
fclose($stream);

Read and write XLSM file in PHP

I want to read and write XLSM type file using PHP. I tried using PHPSpreadsheet for this but it doesn't support XLSM extension.
One possible solution could be to use EasyXLS (https://www.easyxls.com/manual/basics/import-from-xlsx-file-format.html)
// Create an instance of the class that imports XLSM files
$workbook = new COM("EasyXLS.ExcelDocument");
// Import XLSM file
$workbook->easy_LoadXLSXFile("C:\\Samples\\Excel to import.xlsm");
// Get the table of the second worksheet
$xlsSecondTable = $workbook->easy_getSheet("Second tab")->easy_getExcelTable();
// Add more data to the second sheet
$xlsSecondTable->easy_getCell_2("A1")->setValue("Data added by Tutorial37");
for ($column=0; $column<5; $column++)
{
$xlsSecondTable->easy_getCell(1, $column)->setValue("Data " . ($column + 1));
}
// Generate the XLSM file
$workbook->easy_WriteXLSXFile("C:\Samples\Excel with macro.xlsm");
But I was unable to find any Library for this.
Does anyone have any possible solution for this or some other way for this?
function excel($excelfile, $sheet = false){//from stores.blade.php
$tempfile = resource_path("uploads/excel.xlsm");//it just needs a place to store the XML file temporarily, this function only works in Laravel, replace with a filename
if($sheet){//load XML file
//$XML = file_get_contents($tempfile);
$XML = simplexml_load_file($excelfile);
$XML = json_decode(json_encode((array)$XML), TRUE);
$excelfile = pathinfo($sheet)['basename'];
if($excelfile == "workbook.xml"){
$RET = [];
foreach($XML["sheets"]["sheet"] as $data){
$RET[ $data["#attributes"]["sheetId"] ] = $data["#attributes"]["name"];
}
return [
'Filename' => $excelfile,
'SheetName' => "sys_workbook",
'SheetData' => $RET,
];
} else if($excelfile == "sharedStrings.xml"){
foreach($XML["si"] as $index => $value){
if(isset($value["t"])){
$value = $value["t"];
} else {
foreach($value["r"] as $index2 => $value2){
if(is_array($value2["t"])){
$value2["t"] = $value2["t"][0];
}
$value["r"][$index2] = $value2["t"];
}
$value = implode("", $value["r"]);
}
if(is_array($value)){
$value = $value[0];
}
$XML["si"][$index] = $value;
}
return [
'Filename' => $excelfile,
'SheetName' => "sys_strings",
'SheetData' => $XML["si"],
];
} else if(isset($XML["sheetPr"])){
return [
'Filename' => $excelfile,
'SheetName' => $XML["sheetPr"]["#attributes"]["codeName"],
'SheetData' => $XML["sheetData"]["row"],
];
}
return false;
} else {//load ZIPped XLSM file
$files = [];
$zip = new ZipArchive;
if ($zip->open($excelfile) === TRUE) {
for($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
if(startswith($filename, "xl/worksheets/") || $filename == "xl/workbook.xml" || $filename = "xl/sharedStrings.xml"){
copy("zip://" . $excelfile . "#" . $filename, $tempfile);
$XML = excel($tempfile, $filename);
if($XML !== false){
$files[ $XML["SheetName"] ] = $XML["SheetData"];
}
}
}
#unlink($tempfile);
$zip->close();
}
var_dump($files);
die();
}
}
I started work on this, I got this far. The problem is, I don't know how the shared strings (sys_strings) are referenced, and you'd need an equation evaluator to handle the functions. I HOPE, the sheets are in order, so Sheet1 becomes the first array value in sys_workbook.
For anyone still looking for a solution to this problem, the following XLSX reader can parse XLSM without a problem. Also saves the headache in interpreting Excel timestamps and data formats. Tried and tested.
I did not test the Writer though, yet!
https://github.com/shuchkin/simplexlsx

How to write a string in pdf file on specific path.. DOMPDF

I have a function which takes two parameters data(html),name. In the function i am trying to save the data in PDF file on my localhost/abc-folder. Unfortunately it runs fine but don't write in specific file or so..
Here is my code.
<?php
function pdf($data, $name) {
if (count($name) > 1) {
$name = "Orders";
} else {
$name = 'Order_'.$name[0]['order_id'];
}
$pdf = new DOMPDF;
$pdf->load_html($data);
$pdf->render();
$str=$pdf->output();
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/aabcd.pdf","wb");
fwrite($fp,$str);
fclose($fp);
}
?>

PHP - Loop through files on server and run script for each file

I will try to explain as well as possible what I'm trying to do.
I have a folder on a server with about 100 xml files. These xml files are content pages with text and references to attachment filenames on the server that will be pushed to a wiki through an API.
It's all working fine 1 XML file at a time but I want to loop through each one and run my publish script on them.
I tried with opendir and readdir and although it doesn't error it only picks up the one file anyway.
Could someone give me an idea what I have to do. I'm very new to PHP, this is my first PHP project so my code is probably not very pretty!
Here's my code so far.
The functions that gets the XML content from the XML file:
<?php
function gettitle($file)
{
$xml = simplexml_load_file($file);
$xmltitle = $xml->xpath('//var[#name="HEADLINE"]/string');
return $xmltitle[0];
}
function getsummary($file)
{
$xml = simplexml_load_file($file);
$xmlsummary = $xml->xpath('//var[#name="summary"]/string');
return $xmlsummary[0];
}
function getsummarymore($file)
{
$xml = simplexml_load_file($file);
$xmlsummarymore = $xml->xpath('//var[#name="newslinetext"]/string');
return $xmlsummarymore[0];
}
function getattachments($file)
{
$xml = simplexml_load_file($file);
$xmlattachments = $xml->xpath('//var[#name="attachment"]/string');
return $xmlattachments[0];
}
?>
Here's the main publish script which pushes the content to the wiki:
<?php
// include required classes for the MindTouch API
include('../../deki/core/dream_plug.php');
include('../../deki/core/deki_result.php');
include('../../deki/core/deki_plug.php');
//Include the XML Variables
include('loadxmlfunctions.php');
//Path to the XML files on the server
$path = "/var/www/dekiwiki/skins/importscript/xmlfiles";
// Open the XML file folder
$dir_handle = #opendir($path) or die("Unable to open $path");
// Loop through the files
while ($xmlfile = readdir($dir_handle)) {
if($xmlfile == "." || $xmlfile == ".." || $xmlfile == "index.php" )
continue;
//Get XML content from the functions and put in the initial variables
$xmltitle = gettitle($xmlfile);
$xmlsummary = getsummary($xmlfile);
$xmlsummarymore = getsummarymore($xmlfile);
$xmlattachments = getattachments($xmlfile);
//Build the variables for the API from the XML content
//Create the page title - replace spaces with underscores
$pagetitle = str_replace(" ","_",$xmltitle);
//Create the page path variable
$pagepath = '%252f' . str_replace("'","%27",$pagetitle);
//Strip HTML from the $xmlsummary and xmlsummarymore
$summarystripped = strip_tags($xmlsummary . $xmlsummarymore, '<p><a>');
$pagecontent = $summarystripped;
//Split the attachments into an array
$attachments = explode("|", $xmlattachments);
//Create the variable with the filenames
$pagefilenames = '=' . $attachments;
$pagefilenamefull = $xmlattachments;
//Create the variable with the file URL - Replace the URL below to the correct one
$pagefileurl = 'http://domain/skins/importscript/xmlfiles/';
//authentication
$username = 'admin';
$password = 'password';
// connect via proxy
$Plug = new DreamPlug('http://domain/#api');
// setup the deki api location
$Plug = $Plug->At('deki');
//authenticate with the following details
$authResult = $Plug->At('users', 'authenticate')->WithCredentials($username, $password)->Get();
$authToken = $authResult['body'];
$Plug = $Plug->With('authtoken', $authToken);
// Upload the page content - http://developer.mindtouch.com/Deki/API_Reference/POST:pages//%7Bpageid%7D//contents
$Plug_page = $Plug->At('pages', '=Development%252f' . $pagetitle, 'contents')->SetHeader('Expect','')->Post($pagecontent);
// Upload the attachments - http://developer.mindtouch.com/MindTouch_Deki/API_Reference/PUT:pages//%7Bpageid%7D//files//%7Bfilename%7D
for($i = 0; $i < count($attachments); $i++){
$Plug_attachment = $Plug->At('pages', '=Development' . $pagepath, 'files', '=' . $attachments[$i])->SetHeader('Expect','')->Put($pagefileurl . $attachments[$i]);
}
}
//Close the XMl file folder
closedir($dir_handle);
?>
Thanks for any help!
To traverse a directory of XML files you can just do:
$files = glob("$path/*.xml");
foreach($files as $file)
{
$xml = simplexml_load_file($file);
$xmltitle = gettitle($xml);
$xmlsummary = getsummary($xml);
$xmlsummarymore = getsummarymore($xml);
$xmlattachments = getattachments($xml);
}
I also recommend you make a minor adjustment to your code so simplexml doesn't need to parse the same file four times to get the properties you need:
function gettitle($xml)
{
$xmltitle = $xml->xpath('//var[#name="HEADLINE"]/string');
return $xmltitle[0];
}
function getsummary($xml)
{
$xmlsummary = $xml->xpath('//var[#name="summary"]/string');
return $xmlsummary[0];
}
function getsummarymore($xml)
{
$xmlsummarymore = $xml->xpath('//var[#name="newslinetext"]/string');
return $xmlsummarymore[0];
}
function getattachments($xml)
{
$xmlattachments = $xml->xpath('//var[#name="attachment"]/string');
return $xmlattachments[0];
}
Try changing your while loop to and see if that helps out better:
while (false !== ($xmlfile = readdir($dir_handle)))
Let me know.
EDIT:
By using the old way, there could have been a directory name that could have evaluated to false and stopped the loop, the way I suggested is considered the right way to loop over a directory while using readdir taken from here

Categories