Image issue inside PDF loop - image

I'm using DOMPDF to generate PDF's that can contain images and I'm having a weird issue with that.
The PDF is generated inside a loop and the amount of PDF's that needs to be generated varies. When there is only 1 PDF generated everything goes well. The image (or logo in this case) is getting inserted beautifully.
But when there are 2 or more PDF's that have to be generated for some reason the image is replaced with a red cross. But: the image in the first PDF generated in that batch works perfectly. So: the image in PDF #1 loads perfectly, the image in PDF #2 and up is getting replaced with a red cross.
I'm using the latest stable release of DOMPDF (v0.5.2). I've also tested with v0.5.1 but that had the same behaviour.
The server I'm testing this on runs on Linux, with PHP 5.3.8. No errors in the error-log about this (and I do have error logging enabled).
--- edit:
The "path" to the image is an absolute URL, not an absolute or relative (PHP) path and with outputting the contents of the loop to the browser (or a normal HTML email, without the PDF attachment) it displays the image (in all mails) perfectly.
--- edit2:
Using a function to flush all buffers at the end of the loop (which, unfortunately, doesn't change anything to my issue):
function flush_buffers()
{
ob_end_flush();
ob_flush();
flush();
ob_start();
}
An example of the loop I'm using:
for ( $i = 0; $i < count($cert); ++$i )
{
// load the email-template
ob_start();
include($template);
$content = ob_get_contents();
ob_end_clean();
// require dompdf
include_once('dompdf/dompdf_config.inc.php');
// set PDF path - inside temp dir
$newpdf = trailingslashit(realpath(sys_get_temp_dir())).$cert[$i]['coupon'].'.pdf';
// replace shortcodes with wanted content
$certificate = preg_replace($shortcodes, $replacements, $certificate);
$certificate = '<html><body>'.$certificate.'</body></html>'."\n";
$dompdf = new DOMPDF();
$dompdf->load_html($certificate);
$dompdf->set_paper($pageformat, $orientation);
$dompdf->render();
save_pdf($newpdf, $dompdf->output()); // save PDF
#flush_buffers();
}
One of the shortcodes is [logo] and that's getting replaced with an img-tag. As I previously mentioned: that image works fine in the first PDF, but not in the other PDF's.

Upgrading to dompdf 0.6beta2 fixed this issue. Thanks Fabien!

Related

uploading images with filepond in an express ejs project

I'm working on a project that uses express.js for backend and the ejs rendering template for frontend. I've uploaded some images using filepond and the images were converted to base64. However, while viewing the output on the browser, the images look as though they're broken (with a small square at the top-left corner).
I need help with getting this fixed. Here's the code for the function to save the images and convert to base64:
function saveCover(book, coverEncoded) {
if (coverEncoded == null) return;
const cover = coverEncoded;
if (cover != null && imageMimeTypes.includes(cover.type)) {
book.coverImage = new Buffer.from(cover.data, "base64");
book.coverImageType = cover.type;
}
}
The problem I believe will be coming from how you are retrieving the image. If the code you've shown above saves the image (i.e you are seeing some data in the database), then you should focus your attention on the code that retrieves the image to display on the webpage.

Show images inside a pdf created with Gloogle Apps Script Blob

I am creating PDF files using blobs in Google Apps Script from a HTML code, but the problem is that HTML code has an image (referenced by "http") but the created pdf can't show it.
This is my code
function createBlobPDF(myMessage,myTitle){
var blobHTML = Utilities.newBlob(myMessage, "text/html", myTitle+ ".html");
var myPDF = blobHTML.getAs("application/pdf");
return myPDF;
}
Any solution? thanks!!
If the images in HTML are loaded from URLs, the converted PDF file doesn't include the images. If you want to include the images in the converted PDF file, please put the images to HTML as base64 data.
In order to confirm this situation, please see the following sample script. This sample script puts an image as URL and base64 data. The result is displayed them to a dialog and created a PDF file.
When you use this script, please copy and paste to the script editor of Spreadsheet.
Sample script :
function myFunction() {
// image
var url = "https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a";
var blob = UrlFetchApp.fetch(url).getBlob();
var b64 = blob.getContentType() + ';base64,'+ Utilities.base64Encode(blob.getBytes());
var html = "URL<img src=\"" + url + "\">Base64<img src=\"data:" + b64 + "\">";
var h = HtmlService.createHtmlOutput(html);
// Open dialog with the images.
SpreadsheetApp.getUi().showModalDialog(h.setWidth(500).setHeight(200), 'Sample');
// Create from HTML to PDF file.
DriveApp.createFile(h.getAs("application/pdf").setName("text.pdf"));
}
Result of dialog :
Both images can be seen.
Result of PDF file :
Only the image of base64 can be seen.
Note :
This is a simple script. So please modify this to your environment.
References :
How to display Base64 images in HTML?
If I misunderstand your question, I'm sorry.
I tested converting html to pdf with image content. It works but in the bottom of a box, the color does not work! How to make the color to appear in the background of a box?
I wanted to show you my little treatment as an example "<>" but it doesn't work because there is no window for code.gs . Shame...
In short, see the final result in pdf, attached
before clicking on the "validate" button, the screen is displayed, see the screenshot below. this is where you "capture" the HTML presentation and then run to convert HTML to PDF.
after clicking on "validate", here is the result, see below :
normally, I would like to have the same result between screen and PDF.
Then, how to do it ?

Magento Importer Images

I'm trying to import images in the magento importer but nothings appearing. I've gone through all the threads and I'm sure I've done everything I'm supposed to do and now I'm at a complete loss for what the problem could be.
I've created a folder called "import" in my "media" folder and placed the image 'blog-1.jpg' in it and my data is saved in a .csv and it looks similar to this: (i've copied the entries manually for readability)
sku - test1
name - test1
description - Test
short_description - Test
status - 1
visibility - 4
tax_class - 2
qty - 1
price - 1
weight - 1
image - /blog-1.jpg
image_label - Hello
small_image - /blog-1.jpg
small_image_label - Hello
thumbnail - /blog-1.jpg
thumbnail_label - Hello
When I "check" my data it says the data is fine and its found a field. Everything else gets imported correctly but when I click on "images" it has no images in there.
First of all your image are store in media>import folder.
Then in your csv file just write in image column /imagename.jpg
It will find same imagename in import folder if image exist then it will upload the image.
I'm sorry to say, but there is more going on behind the scenes with magento images that it simply putting a filename in the database and linking to your image. The cache generation alone is pretty complex. I believe you are going to have a hard time doing it the way you are attempting.
That being said, I do have a suggestion. Since your images are already on the server, I suggest you write a simple php script to tell magento to attach them to the product image. This could be automated, but i'll give you a small example below...
To attach an image, you would simply navigate to the url like this http://yoursite.com/imageattacher.php?sku=YOURSKU&image=yourimagefilename.jpg
The script would be like this... create a file in your magento ROOT and call it imageattacher.php. Upload your images to the magento media import directory. I have not tested this but it should work.
<?php
// Initialize magento for use outside of core
umask(0);
require_once 'app/Mage.php';
Mage::app('admin');
// Get the variables from the URL
$sku = $_GET["sku"];
$imageName = $_GET["image"];
// get the image from the import dir
$import = Mage::getBaseDir('media') . DS . 'import/' . $imageName;
// Load the product by sku
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
// if the product exists, attempt to add the image to it for all three items
if ($product->getId() > 0)
{
// Add the images and set them for their respective usage (the radio button in admin)
$product->addImageToMediaGallery($import,array('image', 'small_image', 'thumbnail'),false,false);
// Make the changes stick
$product->save();
}
?>

Magento 1.7 - Product Import - Images do not load

I have been trying to import products into Magento and images. The products are being imported and created properly but the images to not get imported.
I am aware of the basics of importing as I have done it in the past but cannot figure out what the issue is and what to do next.
some notes how and what I did:
I am aware to add a "/" before the image name
image name is case sensitive
upload images in media/import folder
media folder to be 777
Tried different folders like var/import or
media/catalog/product/import
Removed the .htcaccess file in media
Flushed cache
when i upload manually an image on a product it does show up properly
i tried specifying the _media_attribute_id in a column as 88 maybe
that should help but it didn't
If you have a Mage_Catalog_Model_Product, all you need to do is this:
$fn = '/absolute/path/to/image.jpg';
$types = array('thumbnail', 'small_image', 'image'); // or null if you already have the product's thumbnail, small image, and image set
$move = true; // move the source file, don't leave it sitting around
$exclude = false; // enabled in product view
$label = 'Turquoise';
$product->addImageToMediaGallery($fn, $types, $move, $exclude); // Strictly speaking, only the first parameter is required
if(!empty($label)) {
$gallery = $product->getData('media_gallery');
$gallery_img = array_pop($gallery);
$gallery_img['label'] = $label;
array_push($gallery['images'], $gallery_img);
$product->setData('media_gallery', $gallery);
}
$product->save();
I have faced the same problem while uploading product image. And i tested many methods to figure that out. But nothing helped me. At last in one of the tutorial I have seen that the image name and the sku name has to be same. with the extension of file extension. So i followed it and by God's grace it worked. You may try that also. It worked for me. I had spent almost one full day to figure that out.

The mPDF does not display images with spaces in the path

I am using MODX.
For example, image with path assets/modules/ZipUpload/Jemchug/mini_820094.jpg displays correctly, but assets/modules/ZipUpload/Duplicate of Kulonyi, Podvesyi/mini_820022.jpg does not.
In the AdjustHTML function of the mpdf.php file there is a line that causes this problem with images:
$html = preg_replace("/[ ]+/",' ',$html);

Resources