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,
Related
I'm using codeigneiter in my system and mpdf 6.1 to generate pdf. When i'm adding new font in ttfont, then i get eror "No font or default font set!", code in my html to print:
$mpdf = new mPDF('utf-8','A4','');
$mpdf->fontDir = './third_party/mPDF/ttfont/'; /* location to save new font */
$mpdf->fontdata['ampleSoft'] = array(
'R' => "ampleSoft-bold.ttf",
'B' => "AmpleSoft-Medium.ttf",
);
$mpdf->SetFont('ampleSoft');
And in file config_fonts i'm adding new code:
'ampleSoft' => array(
'R' => 'AmpleSoft-Medium.ttf',
'B' => 'ampleSoft-bold.ttf'
)
What's wrong with that?
fontDir variable is not available in mPDF 6.x. Define the path in _MPDF_TTFONTPATH constant. You may need to duplicate used font files from the distribution to your custom directory.
https://mpdf.github.io/fonts-languages/fonts-in-mpdf-6-x.html
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
I have a hard time to style a link via the Drupal 8 render structure.
This link needs to be displayed in my custom module:
$add_link = \Drupal::l('<i class="fa fa-cog"></i>' . t('Add new project'), $url);
So between de tags I want a Font awesome icon in front of the text.
But Drupal print all html out as readable text.
I also notice that the l() function is deprecated in Drupal 8.
So what is the best way to do this in the Drupal 8 render structure?
If, like me, you wanted to use a render array of #type => 'link' and include an icon with it then you can do the following:
<?php
use Drupal\Core\Render\Markup;
use Drupal\Core\Url;
$form['actions']['reset_password'] = [
'#type' => 'link',
'#title' => Markup::create('<span class="glyphicon glyphicon-cog"></span> Forgot / Reset Password'),
'#url' => Url::fromRoute('user.pass'),
];
Took me a while, but this should work. Your code has a $url variable which may work fine, but this code below also shows how i got my $url.
$url = new Url(
'entity.eab_contact_entity.edit_form', array(
'eab_contact_entity' => $entity->id(),
)
);
$icon_text = $this->t('<i class="fa fa-pencil"></i>');
$edit_link = \Drupal::service('link_generator')->generate($icon_text, $url);
It turns out that any text that goes into a link needs to be 'safe', so that malicious code cannot be injected etc. If you're curious, it was discussed at length here: https://www.drupal.org/node/2273923
The main point, though, and what makes the above code work for me and answers your question, is the $this->t() surrounding the font-awesome string '<i class="fa fa-pencil"></i>'. That renders it 'safe' and the link generated has the HTML we want, rather than just printing out the text of the HTML.
Finally, in case you are looking for help generating your URL, this tutorial has a lot of hints.
Or if you need to pass spectial chars like or any HTML tag in your link title you can also use the following:
$link_text = Markup::create('<div>' . $node->getTitle() . '</div>');
$link = Link::fromTextAndUrl($link_text, $node->toUrl());
We are building a MediaWiki to deliver content for a conference. There are only a few committee editors so we set $wgRawHtml=true. This enabled us to put iframe tags between html /html tags to properly display googlemaps so they work as expected there. However, we would like to include something like:
<html>
<iframe width="100%" height="600" scrolling="auto" frameborder="1" src="http://oursite.com/index.php?option=com_content&view=article&id=92&Itemid=84">
</iframe>
</html>
to display any of several hundred pages (by changing Itemid value) which contain the formatted text Php output already residing in our Joomla-based site. When I try this (or any other src) I see the frame in MediaWiki, but no content. Is there a way to make this work?
I suspect that there's a permissions or extension problem somewhere. I have access control groups, but they all function as expected (readonly folks, writers, users). My localsettings.php only has
$wgDBname = blah;
$wgSitename = blah;
$wgLocalInterwiki - $wgSitename;
# Skin/logo settings
#$wgDefaultSkin = 'blah';
#$wgLogo = "$wgStylePath/common/images/wiki.png";
# Authentication configuration
#$wgLDAPUseLocal = true;
#$wgLDAPDisableAutoCreate = array( "cauth" => true );
$wgEnableUploads = true;
$wgGalleryOptions = array (
'imagesPerRow' => 4, // Default number of images per-row in the gallery
'imageWidth' => 120, // Width of the cells containing images in galleries (in "px")
'imageHeight' => 120, // Height of the cells containing images in galleries (in "px")
'captionLength' => 20, // Length of caption to truncate (in characters)
'showBytes' => true, // Show the filesize in bytes in categories
'mode' => 'traditional', // One of "traditional", "nolines", "packed", "packed-hover", "packed-overlay"
);
###########
# and because I only have a few editors I have included html
###########
$wgRawHtml = true;
$configdate = gmdate( 'YmdHis', #filemtime( __FILE__ ) );
$wgCacheEpoch = max( $wgCacheEpoch, $configdate );
# Whitelisted pages
#$wgWhitelistRead = array( "Main Page", "Special:Userlogin" );
Aha! While trying different browsers I noticed that an old IE warned me about mixed secure/insecure elements on the page. Since the wiki is on a university site it is https:// which seems to be stopping the iframes from loading the src! Voila! If I include a secure source it loads. So now I either have to use secure source or unsecure the wiki serving. Thanks ancap!
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;