CakePhp 2.x TCPDF Output Chrome - google-chrome

I am using CakePHP 2.x with tcpdf to create a PDF file. I want to output it now to the browser, without saving.
Layout->pdf.ctp
<?php
header("Content-type: application/pdf");
echo $content_for_layout;
?>
View->pdf_testing.ctp
<?php
App::import('Vendor', 'xtcpdf');
$pdf = new XTCPDF('P', 'mm', 'USLETTER', true, 'UTF-8', false);
$textfont = 'freesans'; // looks better, finer, and more condensed than 'dejavusans'
$pdf->AddPage();
$pdf->setHeaderData('', '', '', 'RMA#100000');
$pdf->SetTitle('Some Text');
$pdf->SetHeaderMargin(20);
$pdf->SetTopMargin(40);
$pdf->setFooterMargin(20);
$pdf->SetAutoPageBreak(True, PDF_MARGIN_FOOTER);
$pdf->SetAuthor('Any Author');
$pdf->SetDisplayMode('real', 'default');
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont($textfont, 'B', 20);
$pdf->Cell(0, 14, "TESTING", 0, 1, 'L');
echo $pdf->Output('filename.pdf', 'I');
?>
For Internet Explorer this works fine and the PDF shows up.
With Chrome i get only very userfriendly output like:
%PDF-1.7 %���� 10 0 obj << /Type /Page /Parent 1 0 R /Las....
Even when i set it to
echo $pdf->Output('filename.pdf', 'F');
to save it as file i still get a "Content Length:20" and, with option "I" for Inline, i always get Content-Type HTML/Text instead of Application/pdf.
Any ideas are very appreciated.
Thanks in advance.

Rather than including the header call in your layout file, try adding the following code to your controller method:
$this->response->type('application/pdf');
Cake sends out headers when it's ready so you shouldn't include them directly in your view/layout files. If you want to set a header you should use the response's header method, for example:
$this->response->header('Location', 'http://example.com');
I'm not sure if this will fix your problem as I can't test it, but I think it has a good shot.

I have same issue and fixed it by disabling auto render in cakephp controller method:
$this->autoRender = false;

Related

Summernote executes escaped html

I fetch data from a MySQL database, the data stored is this:
<p><script>alert('123');</script><br /></p>
When I fetch the data normally I get this as result:
<script>alert('123');</script>
This is fine and works as expected, however when I fetch the data into a textarea which is initialized with Summernote I get an alert like this:
Somehow Summernote converts the escaped html tags to functioning HTML.
How do I fix this?
I have already tried the answer of this question:
Escaped HTML in summernote
It did not work.
Why are you not sanitising data both at the time of storage, and when displayed in the Editor, or outside of the editor? Typically, in my CMS, I don't allow <script/> tags as way to help mitigate users adding potentially dangerous scripts.
That said, there is a PR that is being discussed about how we can best go about fixing this issue. https://github.com/summernote/summernote/pull/3782 information or help would be greatly appreciated to move it along, or even another PR fixing the issue.
I managed to fix it by instead of fetching the data in the textarea fetching it in via jQuery like this:
<textarea name="description" id="description"></textarea>
<script>
$('#description').summernote({
height: 250,
codeviewFilter: false,
codeviewIframeFilter: true,
// toolbar
toolbar: [
['font', ['bold', 'italic', 'underline', 'clear']],
['color', ['color']],
['para', ['ul', 'ol', 'paragraph']],
['view', ['fullscreen', 'codeview', 'help']]
],
}).on("summernote.enter", function(we, e) {
$(this).summernote('pasteHTML', '<br />&VeryThinSpace;');
e.preventDefault();
});
$("#description").summernote("code", "<?php echo $video->getDetails('', $fileName, 'desc'); ?>");
</script>
Now it doesn't convert > and $lt; to <> if it is the script tag.
See more information here:
https://github.com/summernote/summernote/pull/3782#issuecomment-774432392
Using javascript you can easily fix this. It worked for me in a React + Django project. I also used django_summer_note and it was also showing data like yours. Then I got that solution:
//simply just create a function like this which will return your data (which one you used with django_summernote).
const createBlog = () => {
return { __html: blog.description };
};
// now in your HTML(JSX) show your data like this.
<div className='' dangerouslySetInnerHTML={createBlog()} />

Yii2 render image in browser without image tag using yii2-imagine

I'm using yii2-imagine
$imagine = yii\imagine\Image::getImagine();
Imagine->open('path/watermark.jpg')->show('jpg');
My problem is it not show the image, it show that:
����JFIF��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), default quality ��C $.' ",#(7),01444'9=82<.342��C 2!!22222222222222222222222222222222222222222222222222����"�� ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������� ���w!1AQaq"2�B���� #3R�br� $4�%�
Any idea?
You need to use the getImagine() function first to invoke the GD or Imagick which ever available instance then call ->open() and ->show() on the object. Moreover, you need to provide the $options for the image to display too. You can copy paste the following code inside an action in your controller and can see the result image. I just tested on my local system, and it is in working, just remember to provide valid path to the $source variable
use yii\imagine\Image;
$source="/path/to/your/image.jpg";
$imagine = new Image();
$options = array(
'resolution-units' => \Imagine\Image\ImageInterface::RESOLUTION_PIXELSPERINCH,
'resolution-x' => 300,
'resolution-y' => 300,
'jpeg_quality' => 100,
);
echo $imagine->getImagine()->open($source)->show('jpg',$options);
Apart from the above solution that displays the image in the browser if you want to display the image inside the img tag you can base64_encode the raw image data returned from the open() method and provide into the tag like below.
echo '<img src="data:image/jpeg;base64,'.base64_encode($imagine->getImagine()->open($source)).'" >';
Hope it helps

Yii2 mpdf with barcode-generator

I'm using yii2 and installed two extension : mpdf , and yii2-barcode-generator-8-types.
Both of them were installed and configured properly and working well.
But what I can't do is to load barcode into pdf.
Here is my code :
Controller :
public function actionPdf()
{
$pdf = Yii::$app->pdf;
$pdf->content = $this->renderPartial('_pdf');
echo $this->renderPartial('_pdf');exit;
return $pdf->render();
}
View :
<div id="showBarcode"></div>
<?php
use barcode\barcode\BarcodeGenerator as BarcodeGenerator;
$optionsArray = array(
'elementId'=> 'showBarcode',
'value'=> '12345678',
'type'=>'code128',
);
echo BarcodeGenerator::widget($optionsArray);
?>
And it show something like this
but If I try to delete all code inside actionPdf() and just write
return $this->render("_pdf");
it show like this:
Please help!!!
I think your controller should be this
public function actionPdf()
{
$pdf = Yii::$app->pdf;
$pdf->content = $this->renderPartial('_pdf');
return $pdf->render();
}
The row with echo $this->renderPartial('_pdf');exit; don't must be used because it prevents the program to invoke correctly the render of the pdf page. If you use this instruction you displays only the render of "html like code" page like you see in result you posted moreover immediately after this instruction you exit form action without invoking the $pdf->render.

PDF::FromHTML creates empty PDF

I'm using Perls PDF::FromHTML to create a PDF-file. My code looks like this:
open HTML, ">", "file.html";
...
close HTML;
chmod(0777, "file.html");
my $pdf = PDF::FromHTML->new(encoding => 'utf-8');
$pdf->load_file("file.html") or die $!;
$pdf->convert(
Font => 'Arial',
LineHeight => 10,
Landscape => 1
);
$pdf->write_file("file.pdf") or die $!;
Since I had difficulties creating an actual PDF-file at the beginning, I'm now exactly following the synopsis on cpan, which is
my $pdf = PDF::FromHTML->new( encoding => 'utf-8' );
# Loading from a file:
$pdf->load_file('source.html');
# Perform the actual conversion:
$pdf->convert(
# With PDF::API2, font names such as 'traditional' also works
Font => 'font.ttf',
LineHeight => 10,
Landscape => 1,
);
# Write to a file:
$pdf->write_file('target.pdf');
However this creates a PDF-file at the right location, but it only consists of a white page. The HTML-file is complete and looks like it should look. What am I missing?
You need to specify a valid font. The second example has a non valid fontname.
If you installed PDF::API2 you could try to use: 'Helvetica' as fontname.
Please check you HTML file, it should be quite simple (no CSS or javascript).
Regards,

Firebug shows data stream but not calendar?

after many days [9] I have come to a point where I need to either look for another calendar or just give up.. Using fullcalendar I have json-events.php sending mysql data through to my staff_calendar.php page, the datastream shows in firebug, the calendar shows in the page but the data doesnt appear in the actual fullcalendar, can anyone please help?
What could be stopping this data rendering in my page?
xxjson-events.php
<?php
mysql_select_db($database_ghl_portal, $ghl_portal);
$query_rsXXCal = "Select events.* , UNIX_TIMESTAMP(start_date) as start_date, UNIX_TIMESTAMP(end_date) as end_date From events";
$rsXXCal = mysql_query($query_rsXXCal, $ghl_portal) or die(mysql_error());
$row_rsXXCal = mysql_fetch_assoc($rsXXCal);
$totalRows_rsXXCal = mysql_num_rows($rsXXCal);
$result = mysql_query($query_rsXXCal) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$eventsArray = array();
$eventsArray['title'] = $row['title'];
$eventsArray['start'] = $row['start_date'];
$eventsArray['end'] = $row['end_date'];
}
header('Content-type: application/json');
echo json_encode($eventsArray)
?>
This outputs both in browser & firebug as:
{"title":"Visit","start":"1330077690","end":"1330081890"}
calendar.php
script type='text/javascript'>
$(document).ready(function() {
$('#calendar').fullCalendar({
editable: false,
eventSources: [
{
url: '/xxjson-events.php',
async: 'false' // No longer asynchronous
}
],
loading: function(bool) {
if (bool) $('#loading').show();
else $('#loading').hide();
}
}); });
</script>
Thanks to anyone who can shed any light on this, however small it may be!
Well first I suspect it is because
async: 'false'
Should be...
async: false
and also, by default allDayDefault is set to true so if you are not displaying allDay events then you won't actually see them.
If you don't want to show allDay events then add...
allDayDefault: false
...to the initialization. On the other hand if you do want to show allDay events then you need to add another field to your database table called allDay and set it to true or false depending on what the event calls for. Pull it out through the array and it should display. Let me know if this helps.
I HAVE GOT IT! So, there were no errors from fullcalendar parser. I though JSON is a one standard... Unfortunately it isn't (mayby only in my situation :P ).
So, JSON response from servlet looks like below:
{"id":"68","title":"ddd","start":"2012-03-28","end":"2012-03-29"}
I just add "[" and "]" before sent from servlet and it works!
[{"id":"68","title":"ddd","start":"2012-03-28","end":"2012-03-29"}]
I hope it will be helpful for someone like me ;-)