Wednesday, April 6, 2011

To remove a space break between in a PDF -file by a low level code

How can you remove the pagebreak between two pieces of text at the bottom of the pdf file without having the source of the original document?

I use Ubuntu.

--

Just for curiosity: I put the document with the pagebreak to my LaTeX -document. I converted it to PDF by pdflatex. Pdflatex ignores the second page completely. If somebody knows, please let me know how you can insert the second page by includegraphics -command or other command.

From stackoverflow
  • There are two pages in your document. That gray space between the two pieces of text at the bottom of the file is not part of the document. It's actually part of Adobe Reader, or whichever PDF viewer you use, and it's meant to indicate a page break -- in this case a separation between page one and page two.

    So if you want to combine these two pages into one page and remove that gray space, then you'll need to find a PDF library (or another non-library tool) that works in a Linux environment, that will allow you to stitch/merge two pages together to create one page.

    Before you go down that path though, I'd recommend that you try to get a hold of the original document and try to re-create PDF, this time using a larger page size so that you fit all of the content onto one page.

    Masi : This is clearly the best practical solution. However, I am interested in file standards and pdf-system works with pagebreaks at the low-level. This means that you should now the exact code for a pagebreak in PDF -documents to find it in the document. - I did not find it by google.
    Rowan : At a low level there is no code for a page break in PDFs. Each page in a PDF is represented by a page object. The page object is a dictionary which includes references to the page's content and other attributes. The individual page objects are tied together in a structure called the page tree. However, the structure of the page tree is not necessarily related to the logical structure or flow of the document. You can read more about this in the PDF reference - but don't bother modifying the internals of a PDF until you've read the reference. http://www.adobe.com/devnet/pdf/pdf_reference.html

Code in a RSS feed

I am using a feed creator (specifically, Kohana's feed::create()), except some of my text might be like this in the description element

See code below

<?php echo 'example'; ?>

The feed creator is using the SimpleXML Library. Whenever the data is returned (using $xml->asXml()) the html angle brackets inside the description element are converted to HTML entities.

This makes the tags be parsed correctly, useful for p tags and the like. However, in this case - the PHP code won't show up (being surrounded by angle brackets).

My question is - how can I show stuff like this in a RSS feed? How can I display &gt; when it itself is parsed back as <? Does that make sense?

Here is an example of what is being outputted:

<description>&lt;p&gt;some content&lt;/p&gt;&#13;

&lt;p&gt;WITH some code&lt;/p&gt;&lt;p&gt;&lt;?php&#13;
    //test me out!&#13;
?&gt;&lt;/p&gt;&#13;
</description>

(note that is not an error above - the entities are all converted)

What I'd like it to display (in a RSS reader) is

some content

WITH some code

<?php
     //test me out! ?>
From stackoverflow
  • Haven't you try with CDATA tags?

    alex : I tried doing that .... but when I called asXml() the CDATA tags themselves were encoded.
  • All RSS tags contain strings so can't you just do your PHP manipulation prior to setting the tag?

    So instead of saying:

    $xml->description = 'Description <?php echo $var; ?>';
    

    you should be doing:

    $xml->description = 'Description ' . $var;
    

    What is the reason that you want to pass PHP code into your RSS feed? I'm guessing that a lot of feed readers would not execute it anyways.

    alex : I want to **show** it... as in ... **look** at the following code sample. I don't want the code being parsed. I'm guessing ALL feed readers would not execute PHP code... it would be quite a security problem if they did execute it on an online reader or on your local machine.
  • You want the code to actually display in the feed as code, not execute, right? If so, you need to escape it the same way you would if you wanted it to display in HTML, i.e.:

    htmlspecialchars( "<?php echo 'example'; ?>" )
    

    That will result in your feed looking even more garbled than it already does, because the PHP will be double-encoded, once for the RSS XML and again for the HTML contained in the RSS XML.

    alex : I tried that - but I couldn't get it to work. Let me try it again. Thanks for your answer.
    alex : I had to use it twice - I leveraged Kohana's html::specialchars(). It ugly as hell but it works - the line looks like this `$markup = '
    ' . html::specialchars(html::specialchars($code)) . '
    ';`

Array Allocation Subscript Number

Quick question regarding how memory is allocated.

If someone were to allocate 20 chars like this:

char store[20];

does this mean that it allocated 20 char type blocks of memory, or that it allocated char type blocks of memory starting with 0 and ending with 20. The difference is that the first example's range would be from store[0] to store[19], whereas the second example's range would be from store[0] to store[20].

From stackoverflow
  • [0] to [19] (20 elements, that is)

  • The first - char store[20] allocates 20 chars, from 0 to 19.

  • It allocates memory for 20 chars, i.e. valid indices go from 0 to 19.

  • It means it allocated one block of memory large enough to hold 20 chars (from index 0 to 19)

    trikker : Thanks. Setting this as answer because you were first.
    ChristopheD : Hm, I think he was last ;-) But hey, no problem ;-)
    trikker : Yeah you're right. Was thinking backwards.

xml file in soap

My last question about soap series :)

Now, i have xml file which must be send to the soap web service.

Do i have to attach it as file to request OR send as method parameter in request?

What is the best approach? By the way size of file is variable.it can be reached 10mb

sometimes.

Thanks

From stackoverflow
  • I'd look into soap with attachments, or alternatively you could serialise it as byte array and send it as a parameter.

    John Saunders : SwA is obsolete.

Is MySQL appropriate for a read-heavy database with 3.5m+ rows? If so, which engine?

My experience with databases is with fairly small web applications, but now I'm working with a dataset of voter information for an entire state. There are approximately 3.5m voters and I will need to do quite a bit of reporting on them based on their address, voting history, age, etc. The web application itself will be written with Django, so I have a few choices of database including MySQL and PostgreSQL.

In the past I've almost exclusively used MySQL since it was so easily available. I realize that 3.5m rows in a table isn't really all that much, but it's the largest dataset I've personally worked with, so I'm out of my personal comfort zone. Also, this project isn't a quickie throw-away application though, so I want to make sure I choose the best database for the job and not just the one I'm most comfortable with.

If MySQL is an appropriate tool for the job I would also like to know if it makes sense to use InnoDB or MyISAM. I understand the basic differences between the two, but some sources say to use MyISAM for speed but InnoDB if you want a "real" database, while others say all modern uses of MySQL should use InnoDB.

Thanks!

From stackoverflow
  • I've run DB's far bigger than this on mysql- you should be fine. Just tune your indexes carefully.

    InnoDB supports better locking semantics, so if there will be occasional or frequent writes (or if you want better data integrity), I'd suggest starting there, and then benchmarking myisam later if you can't hit your performance targets.

    zombat : +1 - my sentiments exactly. I don't use MyISAM anymore unless I have a specific reason to. It's very capable, I used to work with tables with 20 million+ records with no problems, but the transactional capabilities of InnoDB usually outweigh any performance differences you are likely to need until you encounter specific performance issues.
    Branden Hall : Thanks Tim! Can I ask you to expand on "just tune your indexes carefully"? I get the purpose of indexes, but I'm not sure what the tradeoff is - i.e. why not index everything. I'm also a bit confused by what it means to tune an index - I was under the impression you decided to index a column or not and that was it.
    Tim Howland : When you create an index, you add extra overhead when doing inserts (not much, just a little, but it adds up). If you index everything, then it can really slow things down. Indexes can be based on a single field, the first N bytes (or characters) of a field, or two or more fields. You need to work with the "explain query" tool and benchmark your system to figure out the best mix for your particular data set and the searches you typically run. Check the mysql docs here: http://dev.mysql.com/doc/refman/5.0/en/create-index.html for more info.
    Branden Hall : Thanks so much Tim - that explains a lot!
    Greg Smith : Indexes also take up additional disk space. If you create too many of them, the overhead when doing inserts can be huge--it's not safe to assume it will be minor. The problem is that all those little writes to update many of them involves a lot more seeking over the disk surface, and seeks are very slow on most drives.
  • MyISAM only makes sense if you need speed so badly that you're willing to accept many data integrity issues downsides to achieve it. You can end up with database corruption on any unclean shutdown, there's no foreign keys, no transactions, it's really limited. And since 3.5 million rows on modern hardware is a trivial data set (unless your rows are huge), you're certainly not at the point where you're forced to optimize for performance instead of reliability because there's no other way to hit your performance goals--that's the only situation where you should have to put up with MyISAM.

    As for whether to choose PostgreSQL instead, you won't really see a big performance difference between the two on an app this small. If you're familiar with MySQL already, you could certainly justify just using it again to keep your learning curve down.

    I don't like MySQL because there are so many ways you can get bad data into the database where PostgreSQL is intolerant of that behavior (see Comparing Speed and Reliability), the bad MyISAM behavior is just a subset of the concerns there. Given how fractured the MySQL community is now and the uncertainties about what Oracle is going to do with it, you might want to consider taking a look at PostgreSQL just so you have some more options here in the future. There's a lot less drama around the always free BSD licensed PostgreSQL lately, and while smaller at least the whole development community for it is pushing in the same direction.

    Branden Hall : Thanks Greg, the politics of MySQL definately do scare me a bit. Looks like I should do some reading on PostgreSQL and see if I can fit some ramp up time into my development schedule.
  • Since it's a read-heavy table, I will recommend using MyISAM table type. If you do not use foreign keys, you can avoid the bugs like this and that.

    Backing up or copying the table to another server is as simple as coping frm, MYI and MYD files.

  • If you need to compute reports and complex aggregates, be aware that postgres' query optimizer is rather smart and ingenious, wether the mysql "optimizer" is quite simple and dumb.

    On a big join the difference can be huge.

    The only advantage MySQL has is that it can hit the indexes without hitting the tables.

    You should load your dataset in both databases and experiment the biger queries you intend to run. It is better to spend a few days of experimenting, rather than be stuck with the wrong choice.

Is coding style mostly for readability or are there other factors?

I remember reading in Douglas Crockford's "Javascript the Good Parts" book that there is potential for error with blockless statements because of automatic semicolon insertion.

if (condition)
   foo = true;

vs

if (condition) 
{
   foo = true;
}

In the second the example it will work consistently, in the first example a semicolon will be automatically inserted by the interpreter and can lead to ambiguity in the code. As Douglas points out this is potentially bad and hard to debug, which I agree. But it got me thinking are there examples where coding "style" actually has syntax implications? In other words, examples where failing to follow a certain indentation or apparent style actually results in a bug or error. I suppose Python with its significant whitespace is an example, YML with its requirement for no tabs is another.

Feel free to respond in a wide variety of languages and idioms. I am curious to hear about the paradigm cases. In your answer I would like to know the WHAT and the WHY of the coding style or syntax behavior. I don't want to start any coding style flame wars, just matter of fact scenarios where the uninitiated would get tripped up.

From stackoverflow
  • In Python, the whitespace indentation, rather than curly braces or keywords, delimits the statement blocks. An increase in indentation comes after certain statements; a decrease in indentation signifies the end of the current block.

  • Javascript treats these two cases seperately. You have to use the first

    return {
       // code
    }
    
    return 
    {
       // code
    }
    

    If you do not the interpreter adds semi colons in the wrong places. I think it puts one after the condition. So the second would be read wrongly as.

    return;
    {
       // code
    }
    

    Which is not invalid syntax.

    Gordon Potter : Ah that's it! Now I remember reading this in Douglas Crockford's Javascript the Good Parts book. The issue with the automatic semi colons. One of the "evil" features of Javascript. Thanks!
    belugabob : I was surprised to read this answer, as I'd never come across the problem myself. I was even more surprised by the comment about the Crockford book, as I've just read it and couldn't remember this nugget of info. Couldn't find it by looking quickly through the book, so I modified some code to see for myself, and the behaviour you describe didn't take place. Am I reading this correctly - are you saying that the second way of doing things will cause the code inside the braces to always be executed? You're not confusing this with the 'Block-less statements' on pg 113, are you?
    belugabob : Ah! - now that you've edited it to use 'return' instead of 'if...', it makes more sense. Having said that, why on earth would you use that syntax in the first place - it's horrible (Both versions)
    Andreas Grech : @belugabob, refer to Crockford's book pg.102 "Semicolon Insertion"
    David Raznick : To be honest, I agree, its ugly both ways. I just thought it was the example that Gordon was after.
    belugabob : David, thanks for the page reference, now I know why I didn't remember it - I must have decided that I'd never use that construct anyway, and that remembering that rule was a waste of memory space. ;-)
    Gordon Potter : Yeah, what is the point of the brackets after the return? It is an odd construction. Does this mean you are returning some anonymous object instead of say a single value, or variable? Does seem like a goofy construction. Perhaps this has a use when returning JSON key/value stores. But it seems more clear to name your objects in a variable and return the variable.
    belugabob : Totally. By the way '{}' are braces, '()' are parentheses and '[]' are brackets. Some people refer to them as different types of brackets, but the naming convention that I gave is more common and less confusing.
  • Whitespace is not significant in any of the C family of languages, except to separate certain language tokens. The layout of the source code has no effect on the executable produced by the compiler.

  • It depends on the language. In the curly family C/C++/Java/C#, whitespace is not the restriction as long as your braces are opened and closed properly.

    In languages like VB where paired keywords are used, just because keywords delimit functions you can't have

    Private Sub someFunction() End Sub
    

    But in C, C++ and other curly languages, you can have

    void someFunction() { }
    

    In python, I guess its a completely different matter.

    It all depends on the particular language. As per your specific example, I don't think there is a syntactical or semantic difference between the two.

  • In C++, there's a difference between

    vector<pair<int,int>>
    

    and

    vector<pair<int,int> >
    

    because >> is treated as a single token by the parser.

    anon : YThe difference being the first won't compile with some older compilers. This is fixed in the future standard. The meaning of the construct and the code the compiler generates for it will be the same in both cases.
  • A coding style is not only for readability. There are some other factors like

    • Avoid common language pitfalls (silent fails, exceptions while casting etc.)
    • Ease of maintenance
    • Increase the clarity of code (e.g. private functions always starting loweCase and public beeing PascalCase)
    • Enforcing conventions (e.g. not using multiple inheritance or always inherit public in c++)

    an example for ease of maintenacne is below:

    if(x) return true;
    

    vs.

    if(x) 
    {
       return true;
    }
    

    it is clear the second is easier to maintain, because i can simply go ahead and add a new line and add a call to bla() without having to add brackets before.

  • It is language dependant. For instance in Java

    void someFunction() { }
    

    and

    void someFunction() { 
    }
    

    and

    void someFunction() 
    { 
    }
    

    will have no implication whatsoever. However Sun has enlisted a list of coding conventions if you are interested you could read them here.

    These are mainly for maintainability and readability of code but need not be strictly followed.

    Kevin Boyd : oops the braces have not shown up correctly. in my post I will try to repost later as I'm a bit caught up now.
  • No one has mentioned it before, but one point of style is to write

    if (NULL==p) // ...

    instead of

    if (p==NULL) // ...

    The two are functionally equivalent so it's a matter of style. Many prefer the style at the top, because if you type "=" by mistake instead of "==", the first won't compile whereas the second will, and creates a bug that is hard to find (though some compilers now give you a warning for if (p=NULL)).

    Gordon Potter : Ah, that would be truly evil if you were having a bad day before a deadline. Good point, and nice explanation. Thanks!
    Colin Mackay : It depends on the compiler. The C# compiler demands that the condition evaluate to a Boolean. Accidentally putting if (p=null) won't evaluate to a Boolean so the compiler will reject it. Also, many other compilers that allow non-Boolean expressions to be interpreted in conditionals will warn when an assignment is present.
  • Every time I open a parentheses, brace, single or double quote I always close it and then go back to write the statement, condition, etc... This saves me from quite some possibly big mistakes!

    belugabob : I've noticed other people doing this, as I watched them type - now I know why they do it. Personally, I prefer an editor which does this for me - maybe I've just been spoiled by year of using IntelliJ?
    Alix Axel : Old habits die hard! =)
  • The title wasn't specific to conditional blocks, so this came to mind:

    I don't see it mentioned yet, but one thing to consider is how do you find things with simple tools (like grep and its inferior implementations from windowsland).

    For example consider this (admittedly a bit contrived) example

    class Foo
    // vs.
    class
    Foo
    

    You can find former with regex "class\s+Foo", but for latter you'll have to have a specialized tool that can parse C++/C#/java.

    This applies also in C for function prototypes, some weird people prefer

    void
    bar (void)
    // to 
    void bar(void)
    

    While you can generally assume that if in front of function name are some A-Z characters, then it's very likely definiton/declaration.

    Actually when it comes to ifblocks, then placing the brace has very big impact on debugging code in Visual Studio 200x. When it steps into function/block, it will place text/execution line cursor on opening bracket. So if the bracket happens to be waaaaay on the right side, the code window has to scroll there and buggeritall stays there. Extremely annoying.

    xcramps : It's not weird. Its easier to find the function definition in example #1, using grep, for example. These choices were not made unintentionally.

How to get a JavaScript Object's Class?

I created a Javascript Obj, but how can I get back the Class of that Javascript Obj?

I want something that similar to Java .getClass() method.

From stackoverflow
  • There's no exact counterpart to Java's #getClass() in JavaScript. Mostly that's due to JavaScript being a prototype-based language, as opposed to Java being a class-based one.

    Depending on what you need #getClass() for, there are several options in JavaScript:

    A few examples:

    function Foo() {}
    var foo = new Foo()
    
    typeof(foo) // == "object"
    
    foo instanceof Foo // == true
    foo.constructor // == Foo
    
    Foo.prototype.isPrototypeOf(foo) // == true
    Foo.prototype.bar = function (x) {return x+x}
    foo.bar(21) // == 42
    
    Miles : That should probably be `func.prototype` (yes, functions are objects, but the `prototype` property is only relevant on function objects).
    earl : Yes, good point.
    Christoph : you might also want to mention `instanceof`/`isPrototypeOf()` and the non-standard `__proto__`
    Christoph : ES5 has aditionally `Object.getPrototypeOf()`
    clarkf : For me, foo.constructor yields something different (Chrome 8.0.552.0 dev on Mac OS X): `Function Foo() {}`
    earl : Yes, clarkf, that's `Foo` pretty-printed. The comments don't indicate the return values, but equalities that hold for the return values. So the comment means that `foo.constructor == Foo` holds, which will also be the case for you.
  • Javascript is a class-less languages: there are no classes that defines the behaviour of a class statically as in Java. JavaScript uses prototypes instead of classes for defining object properties, including methods, and inheritance. It is possible to simulate many class-based features with prototypes in JavaScript.

    shambleh : I have often said that Javascript lacks class :)
  • You can get a reference to the constructor function which created the object by using the constructor property:

    function MyObject(){
    }
    
    var obj = new MyObject();
    obj.constructor; // MyObject
    

    If you need to confirm the type of an object at runtime you can use the instanceof operator:

    obj instanceof MyObject // true
    
  • In javascript, there are no classes but i think you whant the constructor name so obj.constructor.toString() will tell you what you need.

  • This function returns either "undefined", "null", or the "class" in [object class] from Object.prototype.toString.call(someObject).

    function getClass(obj) {
      if (typeof obj === "undefined")
        return "undefined";
      if (obj === null)
        return "null";
      return Object.prototype.toString.call(obj)
        .match(/^\[object\s(.*)\]$/)[1];
    }
    
    getClass("")   === "String";
    getClass(true) === "Boolean";
    getClass(0)    === "Number";
    getClass([])   === "Array";
    getClass({})   === "Object";
    getClass(null) === "null";
    // etc...